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

8

43

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

Aspiration answered 5/3, 2011 at 23:47 Comment(0)
S
73

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

Stavropol answered 5/3, 2011 at 23:58 Comment(0)
W
25

For longer sections without FreeMarker markup, use <#noparse>...</#noparse>.

Starting with FreeMarker 2.3.28, configure FreeMarker to use square bracket syntax ([=exp]) instead of brace syntax (${exp}) by setting the interpolation_syntax configuration option to square_bracket.

Note that unlike the tag syntax, the interpolation syntax cannot be specified inside the template. Changing the interpolation syntax requires calling the Java API:

Configuration cfg;
// ...
cfg.setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);

Then FreeMarker will consider ${exp} to be static text.

Do not confuse interpolation syntax with tag syntax, which also can have square_bracket value, but is independent of the interpolation syntax.

When using FreeMarker-based file PreProcessor (FMPP), either configure the setting via config.fmpp or on the command-line, such as:

fmpp --verbose --interpolation-syntax squareBracket ...

This will call the appropriate Java API prior to processing the file.

See also:

Welloiled answered 5/8, 2011 at 7:54 Comment(0)
R
4

Another 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.

Revenuer answered 1/8, 2011 at 12:23 Comment(0)
S
4

I had to spent some time to figure out the following scenarios to escape ${expression} -

  • In Freemarker assignment:

<#assign var = r"${expression}">

  • In html attribute:

<a href="/user/${r"${expression}"}"> Some link </a>

  • In Freemarker concatenation:

<#assign x = "something&"+r"${expression}"/>

Shoveler answered 14/12, 2018 at 18:18 Comment(0)
E
3

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.

Ewald answered 6/3, 2011 at 2:3 Comment(1)
Definitely cleaner. Thanks for the tip!Aspiration
A
2

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

Anima answered 5/3, 2011 at 23:56 Comment(0)
B
1

I can confirm that the

${r"${item.id}"}

is the correct way as an example.

So I kinda full example will look like

<span><a href="/user/user-remove/${r"${item.id}"}"> Remove </a></span>

and the output will be :

<span><a href="/user/user-remove/${item.id}"> Remove </a></span>
Borglum answered 3/11, 2015 at 9:52 Comment(0)
I
0

In the case when you want to use non-raw strings so that you can escape double quotes, apostrophes, etc, you can do the following:

Imagine that you want to use the string ${Hello}-"My friend's friend" inside of a string. You cannot do that with raw strings. What I have used that works is:

${"\x0024{Hello}-\"My friend's friend\""}

I have not escaped the apostrophe since I used double quotes.

Inventory answered 15/1, 2014 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.