My FreeMarker method returns a string with $ {variable} - how do I get FreeMarker to parse this?
I have created a class that implements TemplateMethodModelEx from FreeMarker. Imagine exec () function returns the string: "Hello $ {username}"
I am assigning a class to a method in a data model:
dataModel.put("myMethod", myClassInstance);
dataModel.put("username", "John Doe");
My HTML template looks like this:
<p>${myMethod()}</p>
This means that processing the template produces the following output:
<p>Hello ${username}</p>
Since my data model does have a username value , I would prefer the result to be like this:
<p>Hello John Doe</p>
How can I tell FreeMarker to parse the result of myMethod ()? I tried how ? Eval and interpret and both can't accomplish what I want. Is this possible with FreeMarker?
source to share
You need to remove ${}
from string in order to use ?eval
. Return username
as a string from your method and use ?eval
or get a variable from .vars
.
<p>${classInstance.myMethod()?eval}</p>
or
<p>${.vars[classInstance.myMethod()]}</p>
If you want to return not only the variable name, but an expression string (for example, "Hello $ {username}") from a method, use ?interpret
.
<#assign inlineTemplate = classInstance.myMethod()?interpret>
<@inlineTemplate />
source to share