Underscore templating - changing token markers

元气小坏坏 提交于 2019-12-20 12:46:09

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/9802402/underscore-templating-changing-token-markers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!