Lodash _.templateSettings

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is _.templateSettings?

The default settings object used by _.template() for delimiters (escape, evaluate, interpolate), free-variable imports, and the data variable name. Part of Lodash Properties (stable in 4.x — not deprecated). Warning: _.template compiles executable code — never use untrusted template strings; Lodash plans to remove it in v5.

Remember
_.templateSettings

// Defaults used by _.template(string, [options])
// options on _.template() override these values

escape       // RegExp → <%- value %>  (HTML-escaped)
interpolate  // RegExp → <%= value %>  (raw insert)
evaluate     // RegExp → <% code %>    (run JS)
imports      // Object of free variables (includes _)
variable     // data object name (optional)
NeedUse
Compile a template_.template(string, options)
Change interpolate tags_.templateSettings.interpolate
Change escape tags_.templateSettings.escape
Change evaluate tags_.templateSettings.evaluate
Inject helpers_.templateSettings.imports

How to Use _.templateSettings

Set defaults before compiling, or pass options to _.template() for a one-off override.

  1. Decide which delimiters or helpers you need.
  2. Either assign on _.templateSettings, or pass matching keys in _.template(string, options).
  3. Compile with _.template(string).
  4. Call the compiled function with a data object.
  5. If you changed global settings temporarily, restore the previous values.
PropertyTypeRole
escapeRegExpHTML-escape delimiter (default <%- %>)
interpolateRegExpRaw insert delimiter (default <%= %>)
evaluateRegExpJS execution delimiter (default <% %>)
importsObjectFree variables in templates (includes _)
variableStringData object name (avoids with)

Pseudocode

Pseudocode
settings = merge(_.templateSettings, options)
compiled = compile(templateString, settings)
return function (data) {
    return render(compiled, data, settings.imports)
}

Cheat Sheet

Keep these facts handy when configuring templates.

TopicDetail
Object_.templateSettings
Used by_.template(string, [options])
OverridePer-call options win over settings
Default tags<%= %> / <%- %> / <% %>
ScopeGlobal for that Lodash instance
SecurityTrusted templates only; planned removal in v5
Official docslodash.com/docs — _.templateSettings

Live Preview

Run presets inspired by the official _.template docs (settings restore after each run).

Browser demo via Lodash 4.17.21 CDN. Global settings are restored after each preset.

Result
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g
→ "hello mustache!"

Worked Walkthrough — Custom Interpolate

Trace the official custom delimiters example: change interpolate, compile, render, then restore.

Calculation
save = _.templateSettings.interpolate

_.templateSettings.interpolate = /{{([\s\S]+?)}}/g
compiled = _.template('hello {{ user }}!')
compiled({ user: 'mustache' })
→ "hello mustache!"

_.templateSettings.interpolate = save
// later compiles use defaults again

Lodash Examples

Short, copy-ready snippets from the official _.template docs. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official: Custom Interpolate Delimiters

Change templateSettings.interpolate to Mustache-style {{ }} tags.

Lodash
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// → 'hello mustache!'
Try It Yourself

How It Works

  1. Assign. Override the global interpolate RegExp.
  2. Compile. _.template reads the new setting.
  3. Render. {{ user }} inserts mustache.
  4. Caution. Restore the default afterward if other code expects <%= %>.

Example 2 — Default Interpolate Tags

With defaults, <%= %> inserts values without HTML escaping.

Lodash
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// → 'hello fred!'
Try It Yourself

How It Works

  1. Defaults. No settings change — use built-in interpolate RegExp.
  2. Tag. <%= user %> reads data.user.
  3. Insert. Value is written as-is into the string.
  4. HTML. For untrusted text in HTML, prefer the escape delimiter.

Example 3 — Escape Delimiter (<%- %>)

The escape setting matches tags that HTML-escape values before insert.

Lodash
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// → '<b>&lt;script&gt;</b>'
Try It Yourself

How It Works

  1. Tag. <%- value %> uses the escape delimiter RegExp.
  2. Escape. Lodash runs _.escape on the value.
  3. Result. <script> becomes entity-encoded text.
  4. Use. Prefer this for user text rendered into HTML.

Example 4 — Evaluate Delimiter

Evaluate tags run JavaScript; combine with escape tags for safe lists.

Lodash
var compiled = _.template(
  '<% _.forEach(users, function(user) { %>' +
    '<li><%- user %></li>' +
  '<% }); %>'
);
compiled({ 'users': ['fred', 'barney'] });
// → '<li>fred</li><li>barney</li>'
Try It Yourself

How It Works

  1. Evaluate. <% ... %> runs a _.forEach loop.
  2. Imports. _ is available via templateSettings.imports.
  3. Escape. Each name prints with <%- user %>.
  4. Result. Two list items of escaped text.

Example 5 — variable Option (No with)

Pass variable to name the data object and avoid a with statement.

Lodash
var compiled = _.template(
  'hi <%= data.user %>!',
  { 'variable': 'data' }
);
compiled({ 'user': 'fred' });
// → 'hi fred!'

// compiled.source uses function(data) { ... }
// instead of with(obj) { ... }
Try It Yourself

How It Works

  1. Option. { variable: 'data' } overrides settings for this compile.
  2. Template. Expressions must use data.user, not bare user.
  3. Benefit. Compiled source avoids with — clearer and stricter.
  4. Global. You can also set _.templateSettings.variable.

Edge Cases & Pitfalls

Watch for these when changing _.templateSettings.

security

Never compile untrusted templates

_.template builds executable functions. Use only developer-controlled strings. Lodash documents this as insecure and plans removal in v5.

global

Settings changes stick

Assigning _.templateSettings.interpolate affects later compiles. Prefer per-call options, or save/restore around temporary changes.

names

escape vs interpolate naming

In Lodash, the escape delimiter (<%- %>) HTML-escapes. The interpolate delimiter (<%= %>) inserts raw values. Do not reverse them.

timing

Settings apply at compile time

Changing settings after _.template() does not update already-compiled functions. Recompile to pick up new delimiters.

Browser & Runtime Support

_.templateSettings ships with Lodash full builds (stable in 4.x). It configures _.template, which runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x · Warning: template security

_.templateSettings

Available on the [email protected] namespace object. Works in evergreen browsers and current Node LTS when Lodash is installed. Treat _.template as legacy for untrusted input.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.templateSettings Lodash 4.x

Bottom line: Safe for configuring developer-controlled templates. Prefer per-call options over global mutation. Do not compile untrusted strings — Lodash plans to remove _.template in v5.

Performance Notes

ApproachCostBest for
Compile once, reuseO(n) compile, O(n) renderHot paths / loops
Recompile every callO(n) each timeAvoid in production
Per-call optionsSame compile costIsolated delimiter changes

Changing settings is cheap; compiling is the expensive step. Compile once and reuse the function.

Key Takeaways

  • Role: defaults for _.template() delimiters, imports, and variable.
  • Tags: <%= %> raw, <%- %> escaped, <% %> evaluate.
  • Override: per-call options beat global settings.
  • Warning: trusted templates only; removal planned in Lodash v5.

One line: _.templateSettings holds the default delimiters and options that _.template() uses unless you override them per call.

Frequently Asked Questions

_.templateSettings is the object of default options used by _.template(): escape, evaluate, interpolate RegExps, plus imports and variable.
<%= %> interpolates unescaped values. <%- %> HTML-escapes values (escape delimiter). <% %> evaluates JavaScript without printing by default.
Yes. Options passed to _.template(string, options) take precedence over _.templateSettings for that compile.
Yes. Assignments stick for later compiles in the same Lodash instance. Prefer per-call options, or restore the previous RegExp after a temporary change.
No. Compiling untrusted template strings can execute code. Use only developer-controlled templates and trusted data. Lodash plans to remove _.template in v5.
Use the full build: import _ from "lodash" then _.templateSettings. Modular lodash/template still reads the settings object from its build.

Next: _.templateSettings.escape

Deep dive into the HTML-escape delimiter RegExp.

.escape tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful