Search code examples
javascriptunderscore.jstemplate-engine

What are the settings accepted by the underscore templates?


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 compile 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).


Solution

  • But 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 verbatim
    • escape: regex to match expressions that should be inserted after being HTML escaped
    • evaluate: 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 a with statement

    And is it possible to compile 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.