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
Call
What it does
_.templateSettings.variable
Primary setting.
_.templateSettings.variable = /.../g
Assign a custom RegExp or value before compiling.
Prefer the full Lodash build when using _.template() and _.templateSettings.
Library declarations
Lodash
_.templateSettings.variable
Parameters
Parameter
Description
_.templateSettings.variable
Optional. (String) Global data parameter name
options.variable
Optional. (String) Per-compile override
Render argument
Optional. (Object) Unchanged — still a plain data bag
Return Value
Return Value
Description
*
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!'
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
}
_.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.
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
.variableLodash 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.