Appearance
_.template(string='', options={})
本节目标:理解“_.template(string='', options={})”的核心思路,并能把它用于实际开发或面试表达。 创建一个编译过的模板函数,它可以在“
interpolate”定界符中插入数据属性,在“escape”定界符中插入HTML-escape插值的数据属性,并在“evaluate”定界符中执行JavaScript。数据属性可以作为模板中的自由变量访问。如果给定了一个设置对象,它优先于_.templateSettings值.
Note: 在开发构建
_.template使用sourceURLs以方便调试。 For more information on precompiling templates see
lodash's custom builds documentation.For more information on Chrome extension sandboxes see
Chrome's extensions documentation.
Since
0.1.0
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.
Returns
(Function): Returns the compiled template function.
Example// Use the "interpolate内插" delimiter定界符 to create a compiled编译
template.
varcompiled =
_.template('hello\<%= user
%>!');
//返回一个函数,这个函数接受一个数据对象
compiled({'user':'fred'
});
//=\> 'hello fred!'
// Use the HTML "escape" delimiter to escape data property
values.
varcompiled =
_.template('<b><%-value
%></b>');会对script进行转译
compiled({'value':'\<script\>'
});
//=\> '\<b\><script>\</b\>'
// Use the "evaluate" delimiter to execute JavaScript and generate
HTML.
varcompiled =
_.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.
varcompiled =
_.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.
varcompiled =
_.template('hello${ user
}!');
compiled({'user':'pebbles'
});
//=\> 'hello pebbles!'
// Use backslashes to treat delimiters as plain
text.
varcompiled =
_.template('<%="\\\<%- value %\\\>"
%>');
compiled({'value':'ignored'
});
//=\> '\<%- value %\>'
// Use the `imports` option to import `jQuery` as
`jq`.
vartext = '\<% jq.each(users, function(user) { %\>\<li\>\<%- user %\>\</li\>\<% });
%>';
varcompiled =
_.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.
varcompiled =
_.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.
varcompiled =
_.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;
varcompiled =
_.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+ '\
};\
');> 来自
<https://lodash.com/docs/4.17.15#template>