Lodash _.template() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Creates a compiled template function that can interpolate data, HTML-escape values, and run JavaScript inside delimiters. Part of Lodash String (stable since 0.1.0 — not deprecated). Warning: never compile untrusted user template strings — they become executable functions. Use fixed templates and pass data only.

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.template. Start with this line.

Lodash
_.template(string, [options])
CallWhat it does
_.template(string, [options])Primary signature.
_.template(string)Transform or inspect a string.

Prefer import template from "lodash/template" when you want a smaller bundle.

Library declarations
Lodash
_.template(string, [options])

Parameters

ParameterDescription
stringRequired. Template source to compile
optionsOptional. Custom delimiters, imports, variable, etc.

Return Value

Return ValueDescription
*Compiled template function

Lodash Examples

Example 1 — Official: Interpolate Delimiter

Compile once, then insert data with interpolate tags.

Lodash
var compiled = _.template("hello <%= user %>!");

console.log(compiled({ user: "fred" }));
// 'hello fred!'
Try It Yourself

How It Works

  1. Compile. _.template returns a function.
  2. Data. Pass { user: 'fred' }.
  3. Insert. The interpolate tag becomes fred.
  4. Docs. Matches the official first example.

Example 2 — Official: HTML Escape Delimiter

Use escape tags so markup characters become safe entities.

Lodash
var compiled = _.template("<b><%- value %></b>");

console.log(compiled({ value: "<script>" }));
// '<b>&lt;script&gt;</b>'
Try It Yourself

How It Works

  1. Escape tag. Values are HTML-escaped before insert.
  2. Input. A script-looking string becomes safe text entities.
  3. Why. Prefer this for untrusted strings in HTML.
  4. Docs. Matches the official escape example.

Example 3 — Official: Evaluate Delimiter

Run JavaScript to generate repeated HTML.

Lodash
var compiled = _.template(
  "<% _.forEach(users, function(user) { %>" +
  "<li><%- user %></li>" +
  "<% }); %>"
);

console.log(compiled({ users: ["fred", "barney"] }));
// '<li>fred</li><li>barney</li>'
Try It Yourself

How It Works

  1. Evaluate. The evaluate block runs a _.forEach loop.
  2. Escape each. Each name is inserted with the escape tag.
  3. Output. Two list items are concatenated.
  4. Docs. Matches the official evaluate example.

Example 4 — Official: Internal print Helper

Write output from evaluate blocks with print.

Lodash
var compiled = _.template('<% print("hello " + user); %>!');

console.log(compiled({ user: "barney" }));
// 'hello barney!'
Try It Yourself

How It Works

  1. Evaluate. The block runs as JavaScript.
  2. print. Appends text to the template output.
  3. Result. 'hello barney!' — matches the docs.
  4. Tip. Prefer interpolate tags for simple inserts.

Example 5 — Official: Custom Delimiters

Swap default tags for Mustache-style {{ }}.

Lodash
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;

var compiled = _.template("hello {{ user }}!");

console.log(compiled({ user: "mustache" }));
// 'hello mustache!'
Try It Yourself

How It Works

  1. Settings. Override interpolate with a custom RegExp.
  2. Compile. Template uses {{ user }} instead of default tags.
  3. Render. Same data-driven substitution.
  4. Caution. Global templateSettings affects later compiles — prefer per-call options when possible.

Browser & Runtime Support

_.template is a Lodash String helper available since 0.1.0. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Development builds can attach sourceURLs for easier debugging.

✓ Stable · Lodash 4.x

_.template()

Available in [email protected] full builds and the lodash/template modular import. 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
_.template Since 0.1.0

Bottom line: Safe for trusted templates in production once Lodash is on the page. Prefer import template from "lodash/template" for smaller builds. Never compile untrusted template source.

Frequently Asked Questions

_.template(string, [options]) creates a compiled template function. Call that function with a data object to produce the rendered string. Official: compile 'hello' + interpolate user + '!' with { user: 'fred' } → 'hello fred!'.
Interpolate inserts values. Escape HTML-escapes values. Evaluate runs JavaScript (loops, conditionals).
No. Templates compile to functions and can execute code. Only compile trusted template sources; treat data as untrusted and prefer escape tags for HTML.
No. Compile once, reuse the function. Recompiling in a hot loop wastes CPU.
Yes. Set _.templateSettings.interpolate / escape / evaluate, or pass matching options to _.template.
Use import template from "lodash/template" or require("lodash/template") so bundlers can tree-shake. Full build: import _ from "lodash" then _.template.

Next: _.toLower()

Convert a string to lowercase characters.

_.toLower() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

10 people found this page helpful