Inject functions you control—never user-supplied code.
Fundamentals
What Is _.templateSettings.imports?
_.templateSettings.imports is an object whose properties become local variables inside compiled template functions. The most common entry is _, which exposes the Lodash library so you can write <%= _.max(obj.scores) %> or <% _.forEach(obj.users, function (u) { %> directly in template source.
💡
Beginner tip
Think of imports as “what helpers exist inside my template.” Your render data still comes from the argument you pass to the compiled function—that is controlled separately by variable.
Beyond Lodash, teams add formatters, i18n helpers, or URL builders to imports so templates stay declarative without duplicating utility logic in every tag expression.
Foundation
📝 Syntax
Assign properties on the global settings object or pass them per compile:
javascript
_.templateSettings.imports._ = _;
// Add a custom helper:
_.templateSettings.imports.formatPrice = function (n) {
return "$" + n.toFixed(2);
};
Syntax Rules
Type — plain object mapping string keys to functions, values, or libraries.
Default — Lodash often pre-populates _ in browser builds; set it explicitly in Node.
Template usage — reference imported names directly in tag expressions: _.join(...), formatPrice(...).
Data vs imports — render data uses obj (or your variable name); helpers live on imports.
Compile time — Lodash merges imports when building the render function.
javascript
import template from "lodash/template";
_.templateSettings.imports._ = _;
const tpl = template("The square root of 25 is <%= _.sqrt(25) %>.");
console.log(tpl());
// -> "The square root of 25 is 5"
Map of identifiers available inside compiled template code. Keys become variable names in the generated function body.
{ _: _, formatDate: fn }
imports._Lodash
Standard entry pointing to the Lodash library. Enables _.map, _.filter, _.forEach, and hundreds of other helpers in tags.
imports._ = _
custom keysOptional
Project-specific formatters, constants, or small utilities. Keep them pure and side-effect free when possible.
imports.currency = "USD"
per-compile overrideOptional
Pass { imports: { helper: fn } } to _.template() to extend or replace imports for one template only.
_.template(str, { imports })
Imported names are separate from the data object (obj by default). Use obj for payload fields and _ / custom imports for reusable logic.
Hands-On
Examples Gallery
Practical imports patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
📚 Getting Started
Expose Lodash in template scope and call a helper from an interpolate tag.
Example 1 — Lodash math with _.sqrt()
Assign Lodash to imports._, then call a utility inside an interpolate tag.
javascript
_.templateSettings.imports._ = _;
const tpl = _.template("The square root of 25 is <%= _.sqrt(25) %>.");
console.log(tpl());
// -> "The square root of 25 is 5"
For heavy transforms, compute values in JavaScript before rendering and pass simple fields on obj. Reserve imports for small, reusable helpers.
Compare
📋 imports vs related settings
Setting
Type
What it provides
Example in template
imports
Object
Helpers and libraries in scope
_.sum(obj.items)
variable
String
Name of render data parameter
obj.name
interpolate
RegExp
Value-insertion delimiters
<%= ... %>
evaluate
RegExp
Logic block delimiters
<% _.forEach(...) %>
escape
RegExp
Escape-output delimiters
<%- ... %>
🧠 How imports Works
1
Configure imports object
Set global templateSettings.imports or pass imports in the _.template() options.
Config
2
Compile template source
Lodash builds a function whose body includes bindings for each import key alongside the data parameter.
Compile
3
Reference helpers in tags
Inside interpolate and evaluate tags, call _, formatPrice, or any imported name like normal variables.
Author
=
📦
Render with data object
Pass payload fields on obj; imported helpers stay available on every render call.
Important
📝 Notes
imports is an object, not a single function or RegExp.
imports._ is the conventional key for Lodash—set it explicitly in Node.js builds.
Custom helpers should be trusted code you control, never user callbacks.
Imports are fixed at compile time; changing global imports does not update old render functions.
Data fields live on obj (or your variable name)—do not confuse with import keys.
Keep helpers small; heavy logic belongs in JavaScript before calling the template.
Wrap Up
Conclusion
_.templateSettings.imports extends what your templates can call without reimplementing utilities in every tag. Expose Lodash as _, add project formatters, and use per-template overrides when only one file needs a special helper.
Next, configure value delimiters with interpolate, then fine-tune the data parameter name on the variable page.
Set imports._ = _ at bootstrap in Node or bundled apps
Add small, reusable formatters with clear names like formatPrice
Use per-template imports when helpers are template-specific
Document available import keys in your template style guide
Precompute expensive values in JS when templates get complex
❌ Don’t
Inject user-controlled functions into imports
Assume _ exists without verifying your build sets it
Put render payload data on imports—use the render argument
Mutate global imports after compiling shared templates without recompiling
Overload templates with business logic better handled outside tags
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about imports
Use these points when extending Lodash template scope.
5
Core concepts
📦01
Object map
Keys become scope bindings.
Basics
🔧02
imports._
Lodash in templates.
Default
⚙️03
Custom helpers
formatPrice, badge, etc.
Pattern
🔄04
Per compile
Options.imports override.
Scope
🔒05
Trusted only
No user functions.
Security
❓ Frequently Asked Questions
It is an object of names and values merged into the scope of compiled template functions. The default includes _ for Lodash when available, so you can call _.upperCase(obj.name) inside template tags.
imports is the whole object. imports._ is one property that holds the Lodash library reference. You can add other keys like formatDate or currency for custom helpers.
Often Lodash sets it automatically in browser builds. Explicitly assigning _.templateSettings.imports._ = _ guarantees _ is available when you compile templates in Node or custom bundles.
Inside interpolate tags (value output), escape tags, and evaluate blocks (logic). Example: an interpolate tag calling _.max(obj.scores), or an evaluate block with _.forEach(obj.items, function(item) { ... }).
Yes. Pass { imports: { formatPrice: fn } } as the second argument to _.template(). Those helpers are available only for that compiled function.
Only inject trusted helpers you wrote or vetted. Template tags execute JavaScript at compile and render time—never expose user-controlled functions through imports.
Did you know?
Lodash’s default templateSettings.imports already reserves a slot for _. Explicitly assigning imports._ = _ is still a good habit in Node.js and custom bundles so templates behave the same everywhere.