Underscore.js _.template() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Utility

Definition

Compiles a JavaScript template string into a function you call with data to render text or HTML (official Underscore docs). Stable utility — not deprecated.

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.template. Start with this line.

Underscore
_.template(templateString, [settings])
CallWhat it does
_.template(templateString, [settings])Primary signature.
_.template(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.template(templateString, [settings])

Parameters

ParameterDescription
templateStringRequired. Trusted source to compile
settingsOptional. Override interpolate / escape / evaluate / variable

Return Value

Return ValueDescription
*A render function (also has .source)

Underscore.js Examples

Example 1 — Official Interpolate

Compile a string with <%= … %>, then call it with a data object.

Underscore
var compiled = _.template("hello: <%= name %>");
compiled({name: "moe"});
// → "hello: moe"
Try It Yourself

How It Works

  1. Compile. Underscore turns the string into a render function.
  2. Interpolate. <%= name %> inserts the name property.
  3. Pass data. {name: "moe"} supplies the free variable.
  4. Reuse. Call compiled again with different data — no recompile.

Example 2 — Official HTML-Escape

Escape tags run the value through _.escape so markup becomes text.

Underscore
var template = _.template("<b><%- value %></b>");
template({value: "<script>"});
// → "<b>&lt;script&gt;</b>"
Try It Yourself

How It Works

  1. Escape tag. <%- … %> inserts after _.escape.
  2. Dangerous input. The data value is the string <script>.
  3. Safe output. Angle brackets become &lt; / &gt;.
  4. When to use. Prefer escape for any untrusted data in HTML.

Example 3 — Official print

Inside evaluate tags, print appends to the result string.

Underscore
var compiled = _.template("<% print('Hello ' + epithet); %>");
compiled({epithet: "stooge"});
// → "Hello stooge"
Try It Yourself

How It Works

  1. Evaluate. <% … %> runs JavaScript; it does not insert the statement text.
  2. print helper. Underscore injects print into the template scope.
  3. Build the string. 'Hello ' + epithet uses the data property.
  4. Result. Same idea as interpolate, sometimes clearer for expressions.

Example 4 — Official Mustache-Style Settings

Change delimiters with _.templateSettings (or a per-call settings object).

Underscore
_.templateSettings = {
  interpolate: /\{\{(.+?)\}\}/g
};

var template = _.template("Hello {{ name }}!");
template({name: "Mustache"});
// → "Hello Mustache!"
Try It Yourself

How It Works

  1. Override interpolate. Double braces become the insert delimiter.
  2. Compile. The template string uses {{ name }}, not ERB tags.
  3. Render. Same call pattern: pass {name: "Mustache"}.
  4. Scope note. Assigning _.templateSettings is global — prefer per-call settings in apps.

Example 5 — Official variable Option

Name the data object to skip with and render faster.

Underscore
_.template(
  "Using 'with': <%= data.answer %>",
  {variable: "data"}
)({answer: "no"});
// → "Using 'with': no"
Try It Yourself

How It Works

  1. Name the bag. variable: "data" means read data.answer, not bare answer.
  2. No with. Underscore skips the default with scope.
  3. Faster. Official docs recommend this for render speed.
  4. Same call. Still pass {answer: "no"} as the data object.

Browser & Runtime Support

_.template ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.template()

Available in [email protected] (UMD / ESM). Escape tags since 1.2.0; .source and variable since 1.3.3. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.template(templateString, [settings]) compiles a template string into a function. Call that function with a data object to get the rendered string. The official interpolate sample is hello: moe.
Compile hello plus an interpolate tag for name, then call it with {name: 'moe'} to get "hello: moe". A second official sample HTML-escapes a value inside a bold tag.
Interpolate inserts a value as-is. Escape inserts the same value after _.escape so HTML markup becomes text. Evaluate runs JavaScript (loops, ifs, or print) without inserting the statement itself.
No. Underscore templates can run arbitrary JavaScript by design. Compile only template source and settings from trusted authors. Pass user text as data, and prefer escape tags when the result is HTML.
No. Compile once, keep the function, call it whenever the data changes. Recompiling rebuilds a Function object each time. The compiled function also has a .source string for precompilation.
No. It is a stable Underscore utility. HTML-escape tags arrived in 1.2.0. The source property and variable option arrived in 1.3.3. Current 1.x always returns a function (it no longer accepts an initial data object as a second positional argument for rendering).

Next: _.VERSION

Read the Underscore version string shipped with your build.

_.VERSION tutorial →

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.

12 people found this page helpful