The _.template()
function accepts settings
as a third argument, allowing you to change a few things about how do the templates work, including making the templates more Moustache-like. But is this all the settings can do? Can you provide a full list of keys and their meanings for the settings object? And is it possible to pile settings into a template (since the data
argument goes before settings
, it seems that providing settings along with a template would result in underscore trying to apply a template immediately, assuming settings to be the data).
The _.template()
function accepts settings
as a third argument, allowing you to change a few things about how do the templates work, including making the templates more Moustache-like. But is this all the settings can do? Can you provide a full list of keys and their meanings for the settings object? And is it possible to pile settings into a template (since the data
argument goes before settings
, it seems that providing settings along with a template would result in underscore trying to apply a template immediately, assuming settings to be the data).
2 Answers
Reset to default 7But is this all the settings can do?
Yes, all possible settings
are mentioned in the docs. You can read the annoted source as well.
Can you provide a full list of keys and their meanings for the settings object?
interpolate
: regex to match expressions that should be interpolated verbatimescape
: regex to match expressions that should be inserted after being HTML escapedevaluate
: regex to match expressions that should be evaluated without insertion into the resulting string.variable
: A variable name to access the data as properties, instead of using awith
statement
And is it possible to pile settings into a template?
Yes. Simply pass any falsy value (null
, undefined
, false
, …) for data
and the method will return a template function instead of rendering it right away.
If you check out the annotated source (I highly remend it, there's a lot of great explanations there), the three options for settings are evaluate, interpolate and escape. Here's a link to the templateSettings object:
http://underscorejs/docs/underscore.html#section-131
I'm not sure I fully understand the question about piling settings into a template, but you can set global template settings (so you don't have to pass it in as an argument to the template method, like this:
_.templateSettings = {
interpolate: /<%=([\s\S]+?)%>/g,
evaluate: /<%([\s\S]+?)%>/g
};
Sorry if that didn't answer your question, but hopefully the annotated source will shed some light.