Lodash _.templateSettings

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Properties + 2 Examples
Template configuration

What you’ll learn

  • How _.templateSettings connects to _.template() at compile time.
  • The five properties—three RegExp delimiter patterns plus imports and variable.
  • Which tag type is HTML-safe for user data versus raw-output tags.
  • How to bootstrap global settings once, then compile reusable render functions.
  • Where to open each focused tutorial in the property index below.

Prerequisites

Read _.template() first so compile-once / render-many and the three ERB-style tag families make sense. Basic RegExp literacy helps when customizing delimiters.

  • Template compile flow: const render = _.template(source) then render(data).
  • Trusted sources: never compile template strings supplied by end users—settings do not make untrusted templates safe.
  • obj prefix: default templates reference render data as obj.field unless you change variable.

Key concepts

Lodash borrowed ERB’s three-tag model. Each maps to one templateSettings RegExp; two additional properties control scope and data naming.

interpolate

Value tags with HTML escaping—use for names, comments, and dynamic text.

evaluate

Logic tags for if/else and loops—runs JavaScript without auto-printing.

escape (pattern)

Raw-output tags for trusted HTML—despite the name, not an escape function.

imports + variable

Inject _ and helpers; rename the data parameter from default obj.

📝 Default ERB tag model

Out of the box, Lodash recognizes three delimiter families. Customize each via the matching templateSettings property.

Tag roleSettingDefault delimiterPrints output?HTML escaped?
Logic / control flowevaluateopen-percent (no equals)NoN/A
Value insert (safe)interpolateopen-percent-equalsYesYes
Raw HTML insertescapeopen-percent-minusYesNo
1

Bootstrap global settings

Configure once at application startup, then compile templates. This example switches interpolate to Mustache braces while leaving evaluate on ERB logic tags.

javascript
import template from "lodash/template";

// Optional global customization
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
_.templateSettings.imports._ = _;

const greet = template("Hello, {{ obj.name }}!");
console.log(greet({ name: "John" }));
// -> "Hello, John!"
2

Per-template overrides

Pass an options object as the second argument to _.template() to override any setting without touching globals.

javascript
const render = _.template(
  "Hi, {{ obj.name }}!",
  {
    interpolate: /{{([\s\S]+?)}}/g,
    variable: "obj"
  }
);

Suggested learning path

Work through the property tutorials in this order—the same sequence used in the Lodash docs and this site’s navigation.

  1. Raw vs safe output: escape — understand raw-output tags and XSS.
  2. Logic blocks: evaluate — if/else and loops.
  3. Helpers: imports — expose _ and formatters.
  4. Value syntax: interpolate — Mustache braces and custom delimiters.
  5. Data naming: variable — rename obj to data or viewModel.

Property index

Each row links to a focused tutorial. URLs follow /lodash/template-settings/{property}.

PropertyTypeWhat it controls
_.templateSettings.escapeRegExpDelimiter pattern for raw HTML output tags (open-percent-minus). Inserts values without calling _.escape—trusted markup only.
_.templateSettings.evaluateRegExpDelimiter pattern for logic and control-flow tags (open-percent, no equals). Runs if/else, loops, and statements without printing by default.
_.templateSettings.importsObjectMap of helper names injected into compiled template scope—most commonly _ for Lodash plus custom formatters.
_.templateSettings.interpolateRegExpDelimiter pattern for value-insertion tags (open-percent-equals). Output is HTML-escaped via _.escape—the safe default for user text.
_.templateSettings.variableStringName of the data parameter in tag expressions. Default is obj, so templates reference obj.name unless you rename it.

Pitfalls to avoid

Naming

escape is a RegExp

Do not assign a function to templateSettings.escape. HTML encoding is handled by interpolate tags and the _.escape() utility.

Security

User data on raw tags

Never pipe comments or names through escape-pattern tags—use interpolate for escaped output.

Timing

Stale compiled functions

Changing global settings after _.template() does not update already-compiled render functions—recompile when delimiters change.

❓ Frequently Asked Questions

A global configuration object that _.template() reads when compiling template strings. It holds RegExp patterns for three tag types (escape, evaluate, interpolate), an imports object for helpers, and a variable string naming the render data parameter.
No. Lodash defaults work out of the box for standard ERB-style tags. Customize settings when you need Mustache braces, custom delimiters, injected helpers, or a clearer data variable name.
Yes. Each matches a delimiter style at compile time. imports is an object; variable is a string. Do not assign functions to the RegExp properties.
At compile time when you call _.template(). Change global settings before compiling shared templates, or pass overrides in the second argument for one file only.
Interpolate tags (open-percent-equals) HTML-escape output. Raw-output escape-pattern tags and compiled templates from untrusted users are security risks.
Follow the learning path on this page: escape, evaluate, imports, interpolate, then variable—or jump to any row in the property index.

Summary

  • Scope: _.templateSettings configures how _.template() parses and compiles delimiter tags.
  • Five properties: three RegExp delimiters plus imports (object) and variable (string).
  • Next step: open Lodash _.templateSettings.escape or pick any row from the property index.
Did you know?

Despite its name, templateSettings.escape is a RegExp for raw-output tags—not an escape function. Safe user text uses interpolate tags; the standalone _.escape() utility lives on the string category.

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.

6 people found this page helpful