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 badges1 Answer
Reset to default 16The 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, <b>world!</b>
Not escaped: Hello, <b>world!</b>"