Lodash _.templateSettings.variable

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Template configuration

What You’ll Learn

By the end of this tutorial, you’ll know how to rename the data parameter Lodash injects into _.template() expressions—making templates clearer and avoiding naming conflicts.

01

Default obj

See why Lodash uses obj.property in template tags by default.

02

Rename to data

Set templateSettings.variable for more readable template source.

03

Per-template override

Pass { variable: 'viewModel' } without changing global settings.

04

Pair with delimiters

Use your variable name inside interpolate and evaluate tags.

05

Avoid _ clashes

Keep template data separate from Lodash helpers in imports.

06

Compile timing

Set the name before _.template() runs—already-compiled functions stay unchanged.

What Is _.templateSettings.variable?

_.templateSettings.variable is a string that names the data object parameter available inside compiled template expressions. When you write <%= obj.name %>, the obj part comes from this setting—Lodash default is literally the identifier obj.

💡
Beginner tip

Changing variable does not change what you pass when rendering. You still call render({ name: 'John' }). Only the name you use inside the template string changes—from obj.name to data.name.

Teams rename the variable when obj feels cryptic, when designers expect data or viewModel, or when template helpers from imports make short names like obj easy to confuse with other symbols.

📝 Syntax

Assign a valid JavaScript identifier string to the global settings object (or pass it per compile):

javascript
_.templateSettings.variable = "customName";

// Default Lodash value:
// "obj"

Syntax Rules

  • Type — must be a string identifier, not a RegExp or function.
  • Default"obj" unless you override it globally or per compile.
  • Template source — every expression in interpolate, escape, and evaluate tags must use the chosen name.
  • Render call — unchanged; pass a plain object: render({ name: 'Ada' }).
  • Compile time — Lodash reads variable when _.template() runs.
javascript
import template from "lodash/template";

_.templateSettings.variable = "data";

const tpl = template("<%= data.name %> is <%= data.age %> years old.");
const output = tpl({ name: "John", age: 30 });
// -> "John is 30 years old."

⚡ Quick Reference

GoalSettingTemplate expression
Default behavior// "obj" (built-in)<%= obj.title %>
Readable data namevariable = "data"<%= data.title %>
MVC / MVVM stylevariable = "viewModel"<%= viewModel.user %>
Per-template only_.template(str, { variable: "ctx" })<%= ctx.id %>
With Mustache delimitersvariable = "data" + custom interpolate{{ data.name }}
Logic blockssame variable name<% if (data.active) { %>
Default
obj

Built-in identifier

Type
string

Not a RegExp

Scope
global

Or per compile

Sibling
imports

Helpers in scope

🧰 Property Details

How variable fits into _.templateSettings:

variable String

Identifier for the data object inside compiled template code. Lodash embeds this name when building the render function from your template string.

_.templateSettings.variable = "data"
default: obj Built-in

Out of the box, interpolate tags expect obj.property. No configuration needed if you are fine with that name.

<%= obj.username %>
render argument Plain object

The object you pass to the compiled function becomes the value bound to variable at runtime.

render({ username: "ada" })
per-compile override Optional

Pass { variable: "ctx" } as the second argument to _.template() for one-off naming without touching globals.

_.template(str, { variable: "ctx" })

Every tag type that evaluates JavaScript—interpolate, escape, and evaluate—must reference the same variable name you configured.

Examples Gallery

Practical variable patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Rename the data parameter and render a simple profile string.

Example 1 — Use data instead of obj

Set a descriptive variable name, then reference properties as data.name inside interpolate tags.

javascript
_.templateSettings.variable = "data";

const tpl = _.template("<%= data.name %> is <%= data.age %> years old.");
const rendered = tpl({ name: "John", age: 30 });

console.log(rendered);
// -> "John is 30 years old."
Try It Yourself

How It Works

Lodash compiles the template into a function that receives your object and exposes it as data inside tag expressions. The render call stays a normal plain object.

Example 2 — Default obj (no change needed)

If you never set variable, Lodash expects obj.property in template source.

javascript
// _.templateSettings.variable === "obj" by default

const tpl = _.template("Hello, <%= obj.name %>!");
console.log(tpl({ name: "Ada" }));
// -> "Hello, Ada!"

// Writing data.name here would fail unless you set variable = "data"
Try It Yourself

How It Works

The default keeps templates compact. Many Lodash docs and snippets assume obj—only rename when clarity or team conventions call for it.

📈 Practical Patterns

Framework-friendly names, per-template overrides, and logic blocks.

Example 3 — viewModel for UI templates

Match naming used in MVVM or component-driven front ends so designers and developers share vocabulary.

javascript
_.templateSettings.variable = "viewModel";

const cardTpl = _.template(
  "<article><h2><%= viewModel.title %></h2>" +
  "<p><%= viewModel.summary %></p></article>"
);

console.log(cardTpl({
  title: "Release notes",
  summary: "Version 2.0 ships today."
}));

Example 4 — Per-template override (no global mutation)

One module can use ctx while the rest of the app keeps the global default.

javascript
const reportTpl = _.template(
  "Report #<%= ctx.id %>: <%= ctx.status %>",
  { variable: "ctx" }
);

const defaultTpl = _.template("User: <%= obj.name %>");

console.log(reportTpl({ id: 42, status: "complete" }));
// -> "Report #42: complete"
Try It Yourself

How It Works

The options object merges with global templateSettings for that single compile. Other templates compiled elsewhere still use the global variable value.

Example 5 — Same variable in logic and output tags

evaluate blocks must use the same identifier as interpolate tags.

javascript
_.templateSettings.variable = "data";

const tpl = _.template(
  "<% if (data.premium) { %>" +
  "Welcome, <%= data.name %> (Premium member)" +
  "<% } else { %>" +
  "Hello, <%= data.name %>." +
  "<% } %>"
);

console.log(tpl({ name: "Alex", premium: true }));
// -> "Welcome, Alex (Premium member)"

🚀 Beyond the Basics

Working with imports and keeping template scope clear.

Example 6 — Clear separation from imports._

When imports expose Lodash as _, a distinct data name like data avoids mental overlap with helper calls.

javascript
_.templateSettings.variable = "data";
_.templateSettings.imports._ = _;

const summaryTpl = _.template(
  "Items: <%= data.items.length %> | Total: <%= _.sum(data.items) %>"
);

console.log(summaryTpl({ items: [10, 20, 5] }));
// -> "Items: 3 | Total: 35"

When to prefer a custom name

data holds your payload; _ holds helpers. That split makes templates easier to scan during code review.

🧠 How variable Works

1

Read identifier from settings

_.template() loads variable from global settings or the per-call options object (default obj).

Config
2

Embed name in compiled code

Expressions inside delimiter tags are wrapped in a function that receives your data under the chosen parameter name.

Compile
3

You write matching template source

Template strings must reference data.title (or whatever name you picked) in every tag expression.

Author
=

Render with a plain object

Call render({ title: 'Hi' })—Lodash binds it to your variable name and evaluates each tag.

📝 Notes

  • variable is a string identifier, not a RegExp like interpolate.
  • Default name is obj—template source and setting must agree.
  • Changes apply at compile time; re-run _.template() after renaming.
  • All tag types (interpolate, escape, evaluate) share the same variable name.
  • Pick a valid JavaScript identifier—no spaces or reserved words.
  • Renaming does not change the shape of the object you pass to the render function.

Conclusion

_.templateSettings.variable lets you choose the name of the data object inside Lodash template expressions. Stick with default obj for minimal setup, or switch to data, viewModel, or another clear identifier when readability and team conventions matter.

Set it globally at bootstrap, override per compile when one file needs a different name, and keep it aligned with your interpolate and evaluate tags. That completes the core _.templateSettings configuration story.

💡 Best Practices

✅ Do

  • Choose descriptive names like data, viewModel, or ctx
  • Set variable once at application startup before compiling shared templates
  • Document the chosen name in your template style guide
  • Use per-template overrides when only one module needs a different name
  • Keep the same name across interpolate and evaluate tags in one template

❌ Don’t

  • Mix obj.name and data.name in the same template after renaming
  • Use variable = "_"—that collides with typical Lodash imports
  • Expect already-compiled render functions to pick up a renamed global setting
  • Assume changing variable changes the render call signature
  • Use invalid identifiers (spaces, hyphens, reserved words)

Key Takeaways

Knowledge Unlocked

Five things to remember about variable

Use these points when naming the data parameter in Lodash templates.

5
Core concepts
📦 02

Default obj

Built-in unless overridden.

Default
💬 03

data / viewModel

Common renames.

Pattern
⚙️ 04

Compile time

Set before _.template().

Timing
🔄 05

Override

Per-call options.

Scope

❓ Frequently Asked Questions

It sets the name of the data object parameter available inside compiled template expressions. The default is obj, so tags typically reference obj.name. Change it to data and write data.name instead.
No. variable is a string—the identifier Lodash injects into the compiled render function. interpolate, escape, and evaluate are RegExp patterns for delimiter tags.
obj. Unless you override templateSettings.variable or pass variable in the _.template() options, expressions should use obj.propertyName.
When obj feels unclear, when your team prefers data or viewModel, or when obj clashes with another symbol in template scope (for example imports that expose _).
No. You still call render({ name: 'Ada' }). Only the identifier inside template source changes—from obj.name to data.name.
Yes. Pass { variable: 'data' } as the second argument to _.template(). That compile uses your name without mutating global templateSettings.
Did you know?

Lodash defaults to obj because early template compilation used a with statement to expose properties. Even when you pass a plain object today, the compiled function still expects you to prefix fields with whatever name variable holds—usually obj or your custom rename.

Practice variable naming in the Live Editor

Switch from obj to data, run the examples, and try a per-template override.

Open Try It editor →

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.

6 people found this page helpful