How to output ${expression} in Freemarker without

2019-01-14 11:24发布

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

8条回答
三岁会撩人
2楼-- · 2019-01-14 12:00

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>
查看更多
放荡不羁爱自由
3楼-- · 2019-01-14 12:04

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.

查看更多
登录 后发表回答