JSP 中的 Underscore.js 模板
Underscore.js 模板 使用 <%= %>用于变量插值。不幸的是,这也在 JSP(或 GSP)中进行解释。有没有办法在 JSP 中使用 Underscore.js 模板?
Underscore.js templates use <%= %> for variable interpolation. Unfortunately that is also interpreted in a JSP (or GSP). Is there a way to use Underscore.js templates within JSPs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 jsp 页面中添加以下插值和评估设置
,然后您可以使用
<@ @>
而不是<% %>< 编写限定下划线变量、if 和 for 语句/code> 不会与jsp冲突
Add the following interpolate and evaluate settings in your jsp page
then you can write your qualify underscore variables,if and for statements with
<@ @>
instead of<% %>
and will not conflict with jsp@coderman 的示例很有帮助,但不幸的是,如果您想在模板中使用换行符,它就不起作用。例如:
问题是
evaluate
的正则表达式不会跨换行符匹配,如下所述:JavaScript 正则表达式多行标志不起作用因此,对我有用的解决方案是:
注意:
[\s\S] 在评估正则表达式中。这就是关键。
@coderman's example was helpful, but, unfortunately, it doesn't work if you want to use newlines in your templates. For example:
The problem is that the regex for
evaluate
won't match across newlines as described here: JavaScript regex multiline flag doesn't workSo, the solution that worked for me is:
NOTE:
[\s\S]
in the evaluate regexp. That's the key.根据您链接到的网页:
它建议您更改
interpolate
和evaluate
正则表达式。这意味着您可以更改 <%= %>用法与 JSP 不冲突。
According to the webpage you linked to:
It suggests you change the
interpolate
andevaluate
regexes.This means you can change the <%= %> usage to something that doesn't conflict with JSP.
该问题可以通过在代码中转义
<%
序列来解决:因此您不需要更改任何模板引擎逻辑。
The problem can be solved by escaping
<%
sequence in code:So you don't need to change any template engine logics.
不需要全局替换的另一个选项是指定插值并评估特定方法调用
Another option that doesn't require global replace is to specify the interpolate and evaluate to specific method invocation