Lodash _.templateSettings.variable

Beginner
5 min read
Updated: Nov 2026
5 examples
Template

Definition

A string that names the data object inside compiled _.template() source. Set it to "data" and write data.user. When unset, Lodash uses a with statement so bare names work.

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

Syntax

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

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

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

Library declarations
Lodash
_.templateSettings.variable

Parameters

ParameterDescription
_.templateSettings.variableOptional. (String) Global data parameter name
options.variableOptional. (String) Per-compile override
Render argumentOptional. (Object) Unchanged — still a plain data bag

Return Value

Return ValueDescription
*String (identifier)

Lodash Examples

Example 1 — Default: Bare Names via with

With variable unset, properties are free names in the template.

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

How It Works

  1. Unset variable → compiled source uses with.
  2. Bare user resolves from the data object.
  3. Render still receives a plain object.
  4. Tradeoff. Convenient, but with is weaker for CSP / clarity.

Example 2 — Official: variable = 'data'

Qualify every property with the chosen name.

Lodash
_.templateSettings.variable = 'data';

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

How It Works

  1. Set the global variable name to 'data'.
  2. Write data.user in the interpolate tag.
  3. Pass { user: 'barney' } at render time (not { data: … }).
  4. No with — clearer scope for CSP-friendly compiles.

Example 3 — Per-Compile options.variable

Name the argument for one template without a global change.

Lodash
var compiled = _.template(
  'Hi <%= view.name %>',
  { variable: 'view' }
);
compiled({ name: 'Ada' });
// → 'Hi Ada'
Try It Yourself

How It Works

  1. Pass { variable: 'view' } to _.template.
  2. Tags must use view.name.
  3. Globals stay unset for other compiles.
  4. Prefer this when only some templates need a named argument.

Example 4 — Missing Qualifier Breaks

With variable set, bare names are not in scope.

Lodash
var ok = _.template(
  '<%= data.user %>',
  { variable: 'data' }
);
ok({ user: 'sam' });
// → 'sam'

// Bare name fails when variable is set:
try {
  _.template('<%= user %>', { variable: 'data' })({ user: 'sam' });
} catch (e) {
  // ReferenceError: user is not defined
}
Try It Yourself

How It Works

  1. Qualified data.user works.
  2. Bare user is undefined in the compiled function.
  3. Render throws a ReferenceError at call time.
  4. Rule. Once you set variable, always prefix properties.

Example 5 — variable + Mustache Interpolate

Combine a named data object with custom value delimiters.

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

How It Works

  1. variable makes the argument data.
  2. interpolate switches tags to {{ … }}.
  3. Expression is still data.user inside the braces.
  4. One options object keeps globals untouched.

Browser & Runtime Support

_.templateSettings.variable is a Lodash templateSettings property used by _.template. It runs anywhere Lodash 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

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

_.templateSettings.variable

Available with [email protected] full builds that include _.template. Works in evergreen browsers and current Node LTS when Lodash is installed.

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

Bottom line: Prefer setting variable (or options.variable) to avoid with and clarify scope. Use trusted templates only. Note: _.template is planned for removal in Lodash v5.

Frequently Asked Questions

It is a string naming the data object inside compiled templates. Set it to data and write data.user instead of relying on the default with statement.
Unset (empty). Lodash then uses a with statement so you can write user directly. Setting variable disables with and requires a qualified name.
No. It is a string identifier. interpolate, escape, and evaluate are RegExps.
No. You still call compiled({ user: 'barney' }). Only the name inside the template source changes.
Yes. Pass { variable: 'data' } to _.template() without mutating the global setting.
Templates compiled with a variable option avoid the with statement, which helps Content-Security-Policy setups that ban unsafe-eval-style patterns. Still never compile untrusted template strings; Lodash plans to remove _.template in v5.

Next: _.attempt()

Invoke a function safely and get a value or an Error.

_.attempt() 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.

11 people found this page helpful