cancel
Showing results for 
Search instead for 
Did you mean: 

How to call freemarker function with param from javascript

rptapas
Champ in-the-making
Champ in-the-making
Hi,
I am calling a FreeMarker function from Javascript function.
function foo(param1,param2){
validation ………
txt = "${report("${param1!'NA'}",param2)}";
document.write(txt);
}

  <#function report paramX paramY>
  <#assign txt="">
<#assign txt=txt+paramX>
MY CODE ……………………….
  <#return (txt)>
</#function>

The out put on the page is "NA".
If the code in JavaScript is changed from  "${report("${param1!'NA'}",param2)}"; to  "${report("param1",param2)}";
The out put on the page is param1 (and not the value contained in param1).
What is the problem? How can I pass the JavaScript variable to the function in FreeMarker? Where am I going wrong?
6 REPLIES 6

mikeh
Star Contributor
Star Contributor
Have you tried removing the quotes from around "param1" ?

Mike

rptapas
Champ in-the-making
Champ in-the-making
Yes I did. Does not work. In fact param1 is outside quotes. I tried to concatenate the string using +param1+. Even this does not work.

mikeh
Star Contributor
Star Contributor
Looking at this again, I'm not really sure what you're trying to achieve - I think you might misunderstand how the page gets rendered.

On the server, Freemarker will get the template and try to render
function foo(param1, param2)
{
   txt = "${report(param1!'NA', param2)}";
   document.write(txt);
}

In doing so, it will call the "report" function with param1 and param2. The mark-up tells Freemarker to replace param1 with "NA" if param1 isn't defined. If this is all your code, then param1 won't be defined and you'll get "NA". Same for param2, but in this case you'll get a Freemarker exception if you ever try to access param2.
<#function report paramX paramY>
   <#assign txt="">
   <#assign txt=txt+paramX>
   <#return txt>
</#function>

Freemarker calls the "report" function with "NA" and a missing second parameter. Your function returns "NA".

The resultant template is therefore:
function foo(param1, param2)
{
   txt = "NA";
   document.write(txt);
}
which is returned to the client. The client (web browser) then runs the JavaScript (presumably via some other code you've not posted) and the "NA" string gets output.

There's no way for the *client side* web browser code to call a *server side* Freemarker function, which is what I think you're assuming will happen?


Thanks,
Mike

rptapas
Champ in-the-making
Champ in-the-making
Hi Mike,
thank you for the reply.
Well the complete code is written in a single .ftl file. Where there is a form (html), on clicking the button, the javascript function is called which validates param1 and param2, if both the values satisfy the required conditions, then the FreeMarker function (which is written in the same .ftl file) is called. If I access param1 and param2 in the function, I get "param1" and "param2" as it is and not the VALUE of variables "param1" and "param2".
That is my issue.

Alternatively is there any other way to achieve the required functionality:
        There is a form (html), on clicking the button, a javascript function is called, which validates param1 and param2. If both the values satisfy the required conditions, then the FreeMarker function (which is written in the same .ftl file) is called.
        These two values, are conditions, example the created date and modified date of an event, in a work flow. Based on these dates, I want to generate a report which will display all the events which satisfy the given conditions.

Thank you,
  Praveen

mikeh
Star Contributor
Star Contributor
I'm afraid you're still not quite understanding.

You cannot call Freemarker code from JavaScript in a web browser. Freemarker renders the HTML on the server - think of as PHP if you're more used to that. Once the HTML has been generated, Freemarker has *no further involvement* in the page. You can verify this yourself by viewing the HTML source from the browser - all the Freemarker code has been rendered as HTML.

You can use JavaScript validation functions before you POST the form back to the server, but in order for Freemarker to "see" the data from the form, you'd need to write a new webscript.

Thanks,
Mike

manan
Champ in-the-making
Champ in-the-making
THANKS Mike….

saves my time

(Only to tell you thanks… i have created account in alfresco Smiley Happy)