Skip to content

template

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

创建一个编译的模板函数,该函数可以在 "interpolate" 分隔符中插入数据属性,在 "escape" 分隔符中插入 HTML 转义数据属性,并在 "evaluate" 分隔符中执行 JavaScript。数据属性可以作为模板中的自由变量访问。如果给出了设置对象,则它优先于 _.templateSettings 值。

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

注意:在开发版本中,_.template 利用 sourceURLs 更易于调试。

¥Note: In the development build _.template utilizes sourceURLs for easier debugging.

有关预编译模板的更多信息,请参阅 lodash 的自定义构建文档

¥For more information on precompiling templates see lodash's custom builds documentation.

有关 Chrome 扩展沙箱的更多信息,请参阅 Chrome 扩展文档

¥For more information on Chrome extension sandboxes see Chrome's extensions documentation.

新增于

¥Since

0.1.0

参数

¥Arguments

  1. [string=''] (字符串):模板字符串。

    ¥[string=''] (string): The template string.

  2. [options={}] (对象):填充长度。

    ¥[options={}] (Object): The options object.

  3. [options.escape=_.templateSettings.escape] (正则表达式):HTML "escape" 分隔符。

    ¥[options.escape=_.templateSettings.escape] (RegExp): The HTML "escape" delimiter.

  4. [options.evaluate=_.templateSettings.evaluate] (正则表达式):"evaluate" 分隔符。

    ¥[options.evaluate=_.templateSettings.evaluate] (RegExp): The "evaluate" delimiter.

  5. [options.imports=_.templateSettings.imports] (对象):作为自由变量导入到模板中的对象。

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

  6. [options.interpolate=_.templateSettings.interpolate] (正则表达式):"interpolate" 分隔符。

    ¥[options.interpolate=_.templateSettings.interpolate] (RegExp): The "interpolate" delimiter.

  7. [options.sourceURL='lodash.templateSources[n]'] (字符串):已编译模板的 sourceURL。

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

  8. [options.variable='obj'] (字符串):数据对象变量名。

    ¥[options.variable='obj'] (string): The data object variable name.

返回

¥Returns

(函数):返回编译后的模板函数。

¥(Function): Returns the compiled template function.

示例

¥Example

js
// Use the "interpolate" delimiter to create a compiled template.
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'

// Use the HTML "escape" delimiter to escape data property values.
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b>&lt;script&gt;</b>'

// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'

// Use the internal `print` function in "evaluate" delimiters.
var compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'

// Use the ES template literal delimiter as an "interpolate" delimiter.
// Disable support by replacing the "interpolate" delimiter.
var compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'

// Use backslashes to treat delimiters as plain text.
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// => '<%- value %>'

// Use the `imports` option to import `jQuery` as `jq`.
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'

// Use the `sourceURL` option to specify a custom sourceURL for the template.
var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);
// => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.

// Use the `variable` option to ensure a with-statement isn't used in the compiled template.
var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;
// => function(data) {
//   var __t, __p = '';
//   __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
//   return __p;
// }

// Use custom template delimiters.
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'

// Use the `source` property to inline compiled templates for meaningful
// line numbers in error messages and stack traces.
fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
  var JST = {\
    "main": ' + _.template(mainText).source + '\
  };\
');

Lodash v4.17 中文网 - 粤ICP备13048890号