Freemarker escaping freemarker

假如想象 提交于 2019-11-30 17:00:24

问题


I'm using freemarker to generate a freemarker template. But I need some way to escape freemarker tags.

How would I escape a <#list> tag or a ${expression} ?


回答1:


You could also use: ${"$"}{expression} if you find the {} nesting confusing.




回答2:


I'm using the alternative syntax feature. I start the template with [#ftl] and use this syntax.

For the expressions I use the string literal feature: ${r"${expression}"}




回答3:


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.




回答4:


You can configure FreeMarker to use [=exp] instead of ${exp} (since 2.3.28), and [#...]/[@...] instead of <#...>|<@...> by setting both the interpolation_syntax and the tag_syntax configuration setting to square_bracket (in the Java API: Configuration cfg; ... cfg.setInterpolationSyntax(Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX) and cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX)). Then the syntax doesn't clash with the default syntax.

There's one tricky case; if the template starts with <#ftl>, then it will switch the tag syntax back to angle_bracket. To counter that, just add a [#ftl] line before it.

See also: https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.html



来源:https://stackoverflow.com/questions/1641240/freemarker-escaping-freemarker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!