Forum Discussion

scottalvis's avatar
scottalvis
Contributor
13 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" />

10 Replies

  • 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!

  • YuriK's avatar
    YuriK
    Khoros Expert
    13 years ago

    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
    13 years ago
    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
    12 years ago

    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>

     

     

  • Henrik's avatar
    Henrik
    Advisor
    12 years ago

    Hi,

     

    Is it possible to pass an array as a parameter? I've made some tests but couldn't manage to make it work.

    For instance, I'd like to pass a thread list built from two boards:

     

    <#assign topics1 = rest("/boards/id/ID1/topics/recent").messages />
    <#assign topics2 = rest("/boards/id/ID2/topics/recent").messages />
    <#assign topics = [topics1,topics2] />
    
    <@component id="paramtest" param=topics />

    Or is it possible to make a single objet from topics1 and topics2?

     

    Thanks!

    Henrik

  • KPNOnline's avatar
    KPNOnline
    Mentor
    10 years ago

    I am curious about the same, because I cannot get it to work..

     

    I want to pass a custom object to a child component, but cannot get it to work..

     

    Is there any real life example that is not based on R.E.S.T. return datas?

  • nibo's avatar
    nibo
    Mentor
    10 years ago

    Hey Adam,

     

    do you know, if it is possible to pass a boolean as a parameter? It tried it, but it is not working, so I'm currently passing a "true" or "false" as a string.

     

    Thanks

    Nikolay

  • luk's avatar
    luk
    Boss
    10 years ago

    KPNOnline i had some success with using JSON to transport data, for example like this

    <@component id="componentid" data='{"key": "value"}' />


    you have to convert this JSON string back to a hash inside the component, this can be done with the ?eval FreeMarker Built-In.