如何在Freemarker中输出${表达式}而不被解释?

发布于 2024-10-20 15:22:45 字数 472 浏览 5 评论 0 原文

我正在尝试将 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 ${?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

岁月静好 2024-10-27 15:22:45

这应该打印 ${person.name}:

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

来自 freemarker 文档

一种特殊的字符串文字是原始字符串文字。在原始字符串文字中,反斜杠和 ${ 没有特殊含义,它们被视为普通字符。要指示字符串文字是原始字符串文字,必须在左引号或撇号引号之前直接放置 r

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

绝不放开 2024-10-27 15:22:45

对于没有 FreeMarker 标记的较长部分,请使用 <#noparse>...

从 FreeMarker 2.3.28 开始,通过设置 将 FreeMarker 配置为使用方括号语法 ([=exp]),而不是大括号语法 (${exp}) square_bracket 的 interpolation_syntax 配置选项。

请注意,与标记语法不同,插值语法无法在模板内指定。更改插值语法需要调用 Java API:

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

然后 FreeMarker 会将 ${exp} 视为静态文本。

不要将插值语法与标签语法混淆,标签语法也可以有 square_bracket 值,但独立于插值语法。

使用基于 FreeMarker 的文件预处理器 (FMPP) 时,可以通过 config.fmpp< 配置设置/code> 或在命令行上,例如:

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

这将在处理文件之前调用适当的 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 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:

说不完的你爱 2024-10-27 15:22:45

另一种选择是使用 #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.

爱,才寂寞 2024-10-27 15:22:45

我不得不花一些时间找出以下场景来转义 ${表达式} -

  • 在 Freemarker 赋值中:

<#assign var = r"${表达式}">

  • 在html属性中:

一些链接

<#assign x = "something&"+r"${表达式}"/>

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}"/>

郁金香雨 2024-10-27 15:22:45

如果 ${ 是您唯一的问题,那么您可以在 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.

生生漫 2024-10-27 15:22:45

您尝试过$$吗?

我从 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

站稳脚跟 2024-10-27 15:22:45

我可以确认这

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

作为示例,

是正确的方法。所以我的完整示例看起来像这样

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

,输出将是:

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

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>
甜心 2024-10-27 15:22:45

如果您想使用非原始字符串以便转义双引号、撇号等,您可以执行以下操作:

想象一下您想在以下内容中使用字符串 ${Hello}-"Myfriend'sfriend"一个字符串。你不能用原始字符串来做到这一点。我所使用的有效方法是:

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

自从使用双引号以来,我没有转义撇号。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文