Lodash _.templateSettings.evaluate

Beginner
5 min read
Updated: Nov 2026
5 examples
Template

Definition

A RegExp that marks tags whose contents are executed as JavaScript by _.template() (default <% ... %>). Part of Lodash Properties (stable in 4.x — not deprecated). Warning: never compile untrusted template strings — evaluate tags run code; Lodash plans to remove _.template in v5.

Set the property globally, or override it per call with options passed to _.template().

Syntax

Use _.templateSettings.evaluate. Start with this line.

Lodash
_.templateSettings.evaluate
CallWhat it does
_.templateSettings.evaluatePrimary setting.
_.templateSettings.evaluate = /.../gAssign a custom RegExp or value before compiling.

Prefer the full Lodash build when using _.template() and _.templateSettings.

Library declarations
Lodash
_.templateSettings.evaluate

Parameters

ParameterDescription
_.templateSettings.evaluateOptional. (RegExp) Pattern for JS logic tags
Default patternOptional. (RegExp) /<%([\s\S]+?)%>/g
Capture groupRequired. JavaScript source inside the tag

Return Value

Return ValueDescription
*RegExp (with a capturing group)

Lodash Examples

Example 1 — Official: Evaluate + Escape List

Evaluate tags run the loop; escape tags print each item safely.

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. <% _.forEach(...) %> starts the loop.
  2. Imports. _ comes from templateSettings.imports.
  3. Escape. <%- user %> prints each name HTML-escaped.
  4. Close. <% }); %> ends the callback and call.

Example 2 — if / else Branches

Evaluate tags choose which markup path to render.

Lodash
var tpl = _.template(
  '<% if (ok) { %>yes<% } else { %>no<% } %>'
);
tpl({ ok: true });
// → 'yes'

tpl({ ok: false });
// → 'no'
Try It Yourself

How It Works

  1. Condition. <% if (ok) { %> opens a branch.
  2. Static text. yes / no sit outside tags as literal output.
  3. Else. The second evaluate block handles the false path.
  4. Data. ok comes from the object passed to the compiled function.

Example 3 — Evaluate Does Not Auto-Print

An expression inside evaluate is run, but its return value is not inserted.

Lodash
_.template('A<% 1 + 2 %>B')({});
// → 'AB'   (no "3")

_.template('A<%= 1 + 2 %>B')({});
// → 'A3B'  (interpolate prints)
Try It Yourself

How It Works

  1. Evaluate. 1 + 2 runs but does not write to the output buffer.
  2. Literals. Only A and B remain → AB.
  3. Interpolate. <%= 1 + 2 %> inserts 3.
  4. Rule. Logic in evaluate; values in escape/interpolate.

Example 4 — Custom Evaluate Delimiters

Override the RegExp per compile for alternate logic tags.

Lodash
var compiled = _.template(
  '{% if (ok) { %}Y{% } else { %}N{% } %}',
  { evaluate: /{%([\s\S]+?)%}/g }
);
compiled({ ok: true });
// → 'Y'
Try It Yourself

How It Works

  1. Option. Per-call evaluate overrides the global setting.
  2. Pattern. Must include one capturing group for the JS source.
  3. Tags. {% ... %} now acts like <% ... %>.
  4. Safer scope. Prefer options over mutating templateSettings globally.

Example 5 — Loop With a Counter

Use evaluate tags to track index while printing rows.

Lodash
var compiled = _.template(
  '<% var i = 0; _.forEach(items, function(item) { i++; %>' +
    '<%- i %>. <%- item %>; ' +
  '<% }); %>'
);
compiled({ items: ['a', 'b', 'c'] });
// → '1. a; 2. b; 3. c; '
Try It Yourself

How It Works

  1. Setup. Evaluate block initializes i and starts forEach.
  2. Increment. i++ runs each iteration inside evaluate.
  3. Print. Escape tags output the counter and item text.
  4. Close. Final evaluate block ends the callback.

Browser & Runtime Support

_.templateSettings.evaluate 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.evaluate

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

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
.evaluate Lodash 4.x

Bottom line: Use evaluate tags for loops and conditionals; print with escape/interpolate tags. Prefer per-call { evaluate } options over global mutation. Never compile untrusted template strings.

Frequently Asked Questions

It is a RegExp that tells _.template() which tags contain JavaScript to execute. The default matches <\x25 ... \x25> logic tags.
A RegExp. Lodash uses it at compile time to find control-flow blocks. Do not assign a callback to evaluate.
Interpolate tags (<\x25= \x25>) evaluate an expression and insert the result. Evaluate tags (<\x25 \x25>) run JavaScript for logic — loops, if/else, setup — without automatically printing a value.
Pair evaluate tags for the loop with escape or interpolate tags for output, e.g. <\x25 _.forEach(users, function(user) { \x25><li><\x25- user \x25></li><\x25 }); \x25>.
Yes. Pass { evaluate: /.../g } to _.template(string, options) to override _.templateSettings.evaluate for that compile only.
No. Compiling untrusted templates can execute code. Use only developer-controlled templates. Lodash plans to remove _.template in v5.

Next: _.templateSettings.imports

Inject free variables (like _) into compiled templates.

.imports tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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