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])
Call
What 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 _.
Name the bag.variable: "data" means read data.answer, not bare answer.
No with. Underscore skips the default with scope.
Faster. Official docs recommend this for render speed.
Same call. Still pass {answer: "no"} as the data object.
Compatibility
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 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
_.templateUnderscore 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).