javascript - 下划线模板 - 更改标记标记

标签 javascript templates underscore.js

开箱即用的下划线模板使用标记 <%= %>对于原始,和<%- %>用于 HTML 转义内容。

我知道您可以使用以下方法更改标记:

_.templateSettings.interpolate = /\{\{(.+?)\}\}/g;

但这与原始内容和转义内容有什么关系?在我看来你最终只有一种类型的标记。还是我忽略了什么?

最佳答案

Underscore.js documentation说这个(强调):

If ERB-style delimiters aren't your cup of tea, you can change Underscore's template settings to use different symbols to set off interpolated code. Define an interpolate regex to match expressions that should be interpolated verbatim, an escape regex to match expressions that should be inserted after being HTML escaped, and an evaluate regex to match expressions that should be evaluated without insertion into the resulting string.

所以你可以给 _.templateSettings 对象一个 escape 属性:

_.templateSettings.escape = /\{\{-(.*?)\}\}/g
>>> compiled = _.template("Escaped: {{- value }}\nNot escaped: {{ value }}")
>>> compiled({value: 'Hello, <b>world!</b>'})
"Escaped: Hello, &lt;b&gt;world!&lt;&#x2F;b&gt;
Not escaped: Hello, <b>world!</b>"

关于javascript - 下划线模板 - 更改标记标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802402/

相关文章:

javascript - 即使 "if"工作,其他工作

javascript - 如何限制对 react-router 中路由的访问?

c++ - 使用 Boost 传递类函数时出现无效模板错误

google-closure-compiler - 将 Closure 编译器与 Underscore.js _.template 一起使用

javascript - 在节点控制台中使用 require 和在 html 中使用脚本标签有什么区别

javascript - dataTables:删除除一个选择输入外的所有输入

javascript - 无法在字符串上创建属性

c++ - 从左值中推导模板返回类型?

C++ 模板类 : No instance of constructor matches the argument list

node.js - 下划线 : async each loop waiting "inside"