An object of free variables imported into every _.template() compile. By default it includes _ (Lodash) so helpers work inside tags without being passed as data. Part of Lodash Properties (stable in 4.x — not deprecated). Warning: never compile untrusted template strings; import only trusted helpers. Lodash plans to remove _.template in v5.
Set the property globally, or override it per call with options passed to _.template().
Syntax
Use _.templateSettings.imports. Start with this line.
Lodash
_.templateSettings.imports
Call
What it does
_.templateSettings.imports
Primary setting.
_.templateSettings.imports = /.../g
Assign a custom RegExp or value before compiling.
Prefer the full Lodash build when using _.template() and _.templateSettings.
Library declarations
Lodash
_.templateSettings.imports
Parameters
Parameter
Description
_.templateSettings.imports
Optional. (Object) Default free variables for compiles
imports._
Optional. (Function/Object) Lodash reference in templates
options.imports
Optional. (Object) Per-compile free variables
Data object
Optional. (Object) Passed at render time — separate from imports
Return Value
Return Value
Description
*
Object of free variables
Lodash Examples
Example 1 — Default _ Import
Full builds expose Lodash as _ inside templates automatically.
Pass a custom library under any name via options.imports.
Lodash
// Official shape (needs jQuery on the page):
// var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
// var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
// Same idea with a tiny each helper:
var jq = {
each: function (arr, fn) {
for (var i = 0; i < arr.length; i++) fn(arr[i]);
}
};
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jq } });
compiled({ 'users': ['fred', 'barney'] });
// → '<li>fred</li><li>barney</li>'
Extend. Add more keys carefully — they become free variables globally.
Compatibility
Browser & Runtime Support
_.templateSettings.imports 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.
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 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
.importsLodash 4.x
Bottom line: Use imports for trusted free variables like _ and formatters. Prefer per-call { imports } for one-offs. Never compile untrusted template strings.
Frequently Asked Questions
It is an object of names and values imported into compiled templates as free variables. By default it includes _ so you can call Lodash helpers inside template tags.
imports._ is the property that holds the Lodash reference. It is why <\x25 _.forEach(...) \x25> works in templates without passing _ in the data object.
Yes. Assign keys on _.templateSettings.imports, or pass { imports: { helper: fn } } to _.template() for a one-off compile.
In evaluate, escape, and interpolate tags — anywhere the compiled template runs JavaScript expressions.
Options.imports are merged for that compile. Pass the helpers you need; keep _ available if your template uses Lodash methods.
No. Compiling untrusted templates can execute code. Import only trusted helpers. Lodash plans to remove _.template in v5.