最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Underscore templating - changing token markers - Stack Overflow

programmeradmin1浏览0评论

Out of the box underscore templating uses the markers <%= %> for raw, and <%- %> for HTML escaped content.

I know you can change the markers using something like:

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

But how does this relate to raw and escaped content? It looks to me like you end up with only one type of marker. Or have I overlooked something?

Out of the box underscore templating uses the markers <%= %> for raw, and <%- %> for HTML escaped content.

I know you can change the markers using something like:

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

But how does this relate to raw and escaped content? It looks to me like you end up with only one type of marker. Or have I overlooked something?

Share Improve this question asked Mar 21, 2012 at 10:11 UpTheCreekUpTheCreek 32.4k36 gold badges154 silver badges223 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

The Underscore.js documentation says this (emphasis added):

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.

So you can just give the _.templateSettings object an escape property:

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

评论列表(0)

  1. 暂无评论