I'm trying to use Freemarker in conjunction with jQuery Templates.
Both frameworks use dollar sign/curly brackets to identify expressions for substitution (or as they're called in freemarker, "interpolations") , e.g. ${person.name}
.
So when I define a jQuery Template with expressions in that syntax, Freemarker tries to interpret them (and fails).
I've tried various combinations of escaping the ${
sequence to pass it through Freemarker to no avail - \${
, \$\{
, $\{
, etc.
Inserting a freemarker comment in between the dollar and the curly (e.g. $<#-- -->{expression}
) DOES work - but I'm looking for a more concise and elegant solution.
Is there a simpler way to get a Freemarker template to output the character sequence ${
?
If
${
is your only problem, then you could use the alternate syntax in the jQuery Templates plugin like this:{{= person.name}}
Maybe a little cleaner than escaping it.
Also, if you have longer sections without FreeMarker markup, you can use
<#noparse>...</#noparse>
.If you have this issue a lot, since FreeMarker 2.3.28 you can configure FreeMarker to use
[=exp]
instead of${exp}
, by setting theinterpolation_syntax
configuration setting tosquare_bracket
(in the Java API:Configuration cfg; ... cfg.setInterpolationSyntax(Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX)
). Then${exp}
is just static text for FreeMarker. Do not confuse this setting with thetag_syntax
setting, which also can havesquare_bracket
value, but is independent of the interpolation syntax (but you may prefer setting both tosquare_bracket
). See also: https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.htmlAnother option is to use #include with parse=false option. That is, put your jQuery Templates into the separate include page and use parse=false so that freemarker doesn't try and parse it.
This would be a good option when the templates are larger and contain double quotes.
I had to spent some time to figure out the following scenarios to escape ${expression} -
This should print ${person.name}:
Did you try
$$
?I found from the Freemarker manual that
${r"${person.name}"}
will print out${person.name}
without attempting to render it.Perhaps you should also take a look at Freemarker escaping freemarker