我正在尝试将 Freemarker 与 jQuery 模板结合使用。
两个框架都使用美元符号/大括号来标识替换表达式(或者在 freemarker 中称为“插值”),例如 ${person.name}
。
因此,当我使用该语法中的表达式定义 jQuery 模板时,Freemarker 尝试解释它们(但失败了)。
我尝试了转义 ${
序列的各种组合,以将其通过 Freemarker 传递,但无济于事 - \${
、\$\{
、 $\{
等。
在美元符号和大写字母之间插入自由标记注释(例如 $<#-- -->{expression}
)确实有效- 但我正在寻找一个更简洁和优雅的解决方案。
有没有更简单的方法让 Freemarker 模板输出字符序列 ${
?
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)
这应该打印 ${person.name}:
来自 freemarker 文档
This should print ${person.name}:
From the freemarker docs
对于没有 FreeMarker 标记的较长部分,请使用
<#noparse>...
。从 FreeMarker 2.3.28 开始,通过设置
将 FreeMarker 配置为使用方括号语法 (
配置选项。[=exp]
),而不是大括号语法 (${exp}
)square_bracket
的 interpolation_syntax请注意,与标记语法不同,插值语法无法在模板内指定。更改插值语法需要调用 Java API:
然后 FreeMarker 会将
${exp}
视为静态文本。使用基于 FreeMarker 的文件预处理器 (FMPP) 时,可以通过
config.fmpp< 配置设置/code> 或在命令行上,例如:
这将在处理文件之前调用适当的 Java API。
另请参阅:
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 theinterpolation_syntax
configuration option tosquare_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:
Then FreeMarker will consider
${exp}
to be static text.When using FreeMarker-based file PreProcessor (FMPP), either configure the setting via
config.fmpp
or on the command-line, such as:This will call the appropriate Java API prior to processing the file.
See also:
另一种选择是使用 #include 和 parse=false 选项。也就是说,将您的 jQuery 模板放入单独的包含页面并使用 parse=false,以便 freemarker 不会尝试解析它。
当模板较大且包含双引号时,这将是一个不错的选择。
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.
我不得不花一些时间找出以下场景来转义 ${表达式} -
I had to spent some time to figure out the following scenarios to escape ${expression} -
如果
${
是您唯一的问题,那么您可以在 jQuery 模板插件中使用替代语法,如下所示:{{= person.name}}
也许比逃避它。
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.
您尝试过
$$
吗?我从 Freemarker 手册中发现
${r"${person.name}"}
将打印出${person.name}
而不会尝试渲染它。也许您还应该看看 Freemarker 转义 freemarker
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
我可以确认这
作为示例,
是正确的方法。所以我的完整示例看起来像这样
,输出将是:
I can confirm that the
is the correct way as an example.
So I kinda full example will look like
and the output will be :
如果您想使用非原始字符串以便转义双引号、撇号等,您可以执行以下操作:
想象一下您想在以下内容中使用字符串 ${Hello}-"Myfriend'sfriend"一个字符串。你不能用原始字符串来做到这一点。我所使用的有效方法是:
自从使用双引号以来,我没有转义撇号。
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:
I have not escaped the apostrophe since I used double quotes.