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])
Call
What 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
Parameter
Description
string
Required. Template source to compile
options
Optional. Custom delimiters, imports, variable, etc.
Return Value
Return Value
Description
*
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!'
Settings. Override interpolate with a custom RegExp.
Compile. Template uses {{ user }} instead of default tags.
Render. Same data-driven substitution.
Caution. Global templateSettings affects later compiles — prefer per-call options when possible.
Compatibility
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 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
_.templateSince 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!'.
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.