Lodash - template method


Advertisements

Syntax

_.template([string=''], [options={}])

Creates a compiled template function that can interpolate data properties in "interpolate" delimiters, HTML-escape interpolated data properties in "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data properties may be accessed as free variables in the template. If a setting object is given, it takes precedence over _.templateSettings values.

Arguments

  • [string=''] (string) − The template string.

  • [options={}] (Object) − The options object.

  • [options.escape=_.templateSettings.escape] (RegExp) − The HTML "escape" delimiter.

  • [options.evaluate=_.templateSettings.evaluate] (RegExp) − The "evaluate" delimiter.

  • [options.imports=_.templateSettings.imports] (Object) − An object to import into the template as free variables.

  • [options.interpolate=_.templateSettings.interpolate] (RegExp) − The "interpolate" delimiter.

  • [options.sourceURL='lodash.templateSources[n]'] (string) − The sourceURL of the compiled template.

  • [options.variable='obj'] (string) − The data object variable name.

Output

  • (Function) − Returns the compiled template function.

Example

var _ = require('lodash');
var compiled = _.template('Hello <%= user %>!');
var result = compiled({ 'user': 'Joe' });

console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

Hello Joe!
lodash_string.htm
Advertisements