Lodash教程

Lodash template method

语法

_.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.

参数

[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.

输出

(Function) − Returns the compiled template function.

实例

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

输出

Hello Joe!
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4