How to output ${expression} in Freemarker without it being interpreted?

Glenn Barnett picture Glenn Barnett · Mar 6, 2011 · Viewed 28.7k times · Source

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 ${?

Answer

javanna picture javanna · Mar 6, 2011

This should print ${person.name}:

${r"${person.name}"}

From the freemarker docs

A special kind of string literals is the raw string literals. In raw string literals, backslash and ${ have no special meaning, they are considered as plain characters. To indicate that a string literal is a raw string literal, you have to put an r directly before the opening quotation mark or apostrophe-quote