Forum Discussion

dustin's avatar
dustin
Expert
10 years ago

How do I access URL query parameters with Freemarker?

After researching online, I found two suggestions, but neither of them seem to work.

I am trying to read the parameter passed in the url...  for example:

http://communityfoo.com?param=false

 

The two suggestions I've found online that didn't work are...

${RequestParameters.param}

-- or --

${request.getParameter("param")}

Any ideas?

  • In Lithium, you have the http.request context object which provides such functionality:

     

    <#if http.request.parameters.name.get("param", "0") == "1">
    
        <#-- do something -->
    
    </#if>

    beware that boolean values are not really passed as such to FreeMarker, it will see the string "false", so you'd have to compare to that, which just makes the code longer. Therefore I prefer to use binary values 0/1 for such things. Your example would look like:

    <#if http.request.parameters.name.get("param", "false") == "true">
    
        <#-- do something -->
    
    </#if>

    <#-- or to output the value directly with a default of "not set" -->
    ${http.request.parameters.name.get("param", "not set")}

    hope that helps!

2 Replies

  • In Lithium, you have the http.request context object which provides such functionality:

     

    <#if http.request.parameters.name.get("param", "0") == "1">
    
        <#-- do something -->
    
    </#if>

    beware that boolean values are not really passed as such to FreeMarker, it will see the string "false", so you'd have to compare to that, which just makes the code longer. Therefore I prefer to use binary values 0/1 for such things. Your example would look like:

    <#if http.request.parameters.name.get("param", "false") == "true">
    
        <#-- do something -->
    
    </#if>

    <#-- or to output the value directly with a default of "not set" -->
    ${http.request.parameters.name.get("param", "not set")}

    hope that helps!