Forum Discussion

Gursimrat's avatar
Gursimrat
Leader
11 years ago

Can I use javascript variable in freemarker

Can I? I read some where that FTL is executed before the JS, so it is not possible?

5 Replies

  • JasonL's avatar
    JasonL
    Lithium Alumni (Retired)
    11 years ago

    Gursimrat wrote:

    Can I? I read some where that FTL is executed before the JS, so it is not possible?


    Hi Gursimrat,

    It's not possible - freemarker templates are processed on the server-side first, before the page gets loaded on client-side

    (javascript code gets executed only after the page is loaded in client-side browser)

    --Jason

  • AdamN's avatar
    AdamN
    Khoros Oracle
    11 years ago

    What kind of data are you trying to get through JavaScript? Perhaps there's another approach that could be used via Freemarker.

  • Gursimrat's avatar
    Gursimrat
    Leader
    11 years ago

    We are creating a search, where we are reading the parameter from the URL, can we feed that to FTL? That is, whatever the user searches, in a custom search.

  • JasonL's avatar
    JasonL
    Lithium Alumni (Retired)
    11 years ago

    Gursimrat wrote:

    We are creating a search, where we are reading the parameter from the URL, can we feed that to FTL? That is, whatever the user searches, in a custom search.


    have you tried this?

    http.request.parameters.name.get("parameter_name", default_value)

    9.1.0

    Returns a parameter set on the request, or the default value specified if the request parameter is not found.

    Example

    ${http.request.parameters.name.get("page", 1)}

     

    Ref:

    https://community.lithium.com/t5/developers-knowledge-base/http-request-FreeMarker-context-object/ta-p/9323

  • sunny_mody's avatar
    sunny_mody
    Advisor
    11 years ago

    if you want to use your javascript variable into freemarker, then probably you should use the endpoint feature

    Create an endpoint

    Try hitting an ajax call to an endpoint with the desired variable as query parameter

    Fetch the parameter in endpoint and use it into your freemarker  over there. (The freemarker code that you wanted to use now should be in your endpoint)

     

    Hope this workaround might help you.