开箱即用的下划线模板使用标记 <%= %>
对于原始,和<%- %>
用于 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, <b>world!</b>
Not escaped: Hello, <b>world!</b>"
关于javascript - 下划线模板 - 更改标记标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802402/