Forum Discussion

scottalvis's avatar
scottalvis
Contributor
12 years ago

Passing parameters / objects to a custom component?

Is it possible to pass parameters / objects to custom components?

 

For example:

 

<@component id="custom-component-id" param="object-or-string-value" />

  • YuriK's avatar
    YuriK
    Khoros Expert

    Hey Scott,

     

    You can do the following:

     

    • In the XML tab for the quilt insert your component as follows:
    <component id="custom-component-id" param="object-or-string-value" />
    •  In your component, you can access the passed in param as follows (The attempt/recover loop is to catch the case where the parameter doesn't exist)
    <#attempt> 
    ${env.context.component.getParameter('param')}
    <#recover>
    Parameter with the name param not found </#attempt>

    This is currently experimental functionality and we will be improving it so please use sparingly for now. Also note that you will not be able to see full functionality in the custom component preview window as the parameter won't be passed in there.

     

    Hope this helps,

     

    Yuri

    • scottalvis's avatar
      scottalvis
      Contributor
      Follow up:

      It appears this only works on simply types such as strings and numbers. Is it possible to pass a list of messages?

      Thanks again,

      Scott
      • AdamN's avatar
        AdamN
        Khoros Oracle

        Hi Scott,

         

        Sorry for the late follow up. I was forwarded to your post by someone else that had the same question. I figured I'd reply here for the benefit of others.

         

        If you’re adding the component to a page via the page editor in Studio, then I believe you’re fairly restricted in what types of values you can use… pretty much just strings as you discovered.

         

        If you’re adding the component to another component via the <@component> macro, then you have a bit more flexibility. With the macro, you can pass other types and objects as well. For example:

        <#assign community=rest("/").community />
        <@component id="paramtest" param=community />

          

        Then in your paramtest component, you could do something like:

        ${env.context.component.getParameter('param').id}

        to give you the id of the community.

         

        Here's a better example more a long the lines of what you were talking about. This example shows passing some threads:

        <#assign threads=rest("/threads/recent").threads />
        <@component id="paramtest" param=threads />

         

        Then in your paramtest component:

        <#assign threads=env.context.component.getParameter('param') />
        <#list threads.thread as thread>
        ${thread.messages.topic.subject}<br/>
        </#list>

         

         

  • Continued:

     

    I would like to do something similar to this:

     

    <@component id="common.widget.user-name" user="conv:${message.author.login}" />

     

    Is this possible in a custom component?

     

    Thanks!