Forum Discussion

lorseau's avatar
13 years ago

Component and parameters

Hello,

 

Is it possible to send/give parameters to a custom component?

 

I also thought to have two components, one with the parameters (variables) and one with the code, but the variables are not recognize on the second one so it doesn't work ;o(

 

Thanks,

Laurent

8 Replies

  • The easiest would probably be to pass on that parameter on client side via JavaScript. But that's probably also the least secure way of doing so.
    As far as I know it's not possible to manipulate session data via freemarker in a component which would be the second way I could think of it to work.

    Maybe you can share some more details of what you roughly want to achieve, e.g. which type of parameters you want to pass on to a component. There might be different ways to get the same result.
  • AdamN's avatar
    AdamN
    Khoros Oracle
    13 years ago

    When you say parameters, are you referring to URL parameters (ie. query string parameters). If so, you can use the http.request context object  to get the value of query string parameters. Here's a simple example

     

    <#assign val = http.request.parameters.name.get("myParameter", "default value") /> 
    myParameter: ${val}

    You can specify your own name for the parameter, as well as whatever default value you'd like to assign if the parameter isn't present in the query string.

     

    Alternately, if you want to parse out the request URL, you can use http.request.url to get the URL of the current page, and then freemarker has many built-ins for strings, such as split, substring, index_of, etc.

     

    Here's more on the built-ins:

    http://freemarker.sourceforge.net/docs/ref_builtins_string.html

     

    And here's more on the http.request context object:

    http://lithosphere.lithium.com/t5/Developer-Knowledge-Base/Context-objects-for-custom-components-http-request/ta-p/9323

     

    I hope this helps!

     

    -Adam

  • for the moment, on the top of my component i have these variables / parameters

     

    <#assign paramBoxTitle             = "What's new in the Community" />
    <#assign BoxType                   = "mgc-object-world" />
    <#assign paramNbItemMax            = 5 />
    <#assign paramPageSize             = 200 />
    <#assign paramBoardTypes           = ["all"] />
    <#assign paramCategories           = ["all"]>
    <#assign paramBoards               = ["Welcome", "Feedback"]>
    <#assign paramExcludeCategories    = [""]>
    <#assign paramExcludeBoards        = [""]>
    <#assign paramBoardId              = "" />

     

    I wanted to know if it's possible to get these values from another component or via the <component id="MEGAWhatsNewForum"/> which is on the page.

     

    Thanks,

    Laurent

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    13 years ago

    Ah, I think I see now. So essentially you have a bunch of variables that you want to use in multiple places. Are these variables final, or do you intend to update the values?

     

    If the values will not be changed, you can use the freemarker "include" directive to insert the component containing your variables into other components.

     

    For example, if you had a component containing all these variables named "global-vars", you could add this to all the components that you wanted to use these variables:

    <#include "global-vars" />

    Keep in mind that this approach will only work if you don't plan to update the values of the variables in "global-vars". If you've included "global-vars" in components A and B, updating the value of one of the variables in A will have no impact on B. Each component essentially gets their own copy of the original code from "global-vars".

     

    If you want to be able to update a variable in component A and have it impact component B, that's going to be more tricky and I'd like to understand more about what you're trying to accomplish.

  • In fact I wanted to do the opposite ;o)

     

    My component has some "parameters"

    For example I can show the recent topics of:

    - a list of categories

    - a list of board types

    - a list of boards

    and I can exclude categories and boards

     

    so the code of the component is always the same. Just the parameters are differents.

     

    BUT I think i have MY Solution ;o)

    I have to create

    - a component with the parameters

    - a component with the code

    - a component which include this two components

     

     

     

     

  • lorseau's avatar
    lorseau
    Ace
    13 years ago

    It works with just 2 components

     

    - one with the code

    - one with the variables + an include to the code

     

    I thought I tested this way last week but looks like I did a mistake

  • AdamN's avatar
    AdamN
    Khoros Oracle
    13 years ago

    Ah, gotcha... So essentially you're treating your code as a function or a macro, and the variables are what you want to pass into it. You may already be aware of this, but FreeMarker includes the ability to create functions and macros:

    http://freemarker.sourceforge.net/docs/ref_directive_function.html

    http://freemarker.sourceforge.net/docs/ref_directive_macro.html

    Functions and macros are very similar to each other, the main difference being that a function in FreeMarker must return a value.

     

    It sounds like you've already got things working for you now, so you might not see much value in converting it to a function or a macro now, but I wanted to throw that out just in case you found it useful!

     

  • No, I didn't know that! I'm new with freemarkers.

    So thanks a lot!