这里有一个有趣的部分http://underscorejs.org/#template
如果您不喜欢ERB样式的分隔符,可以更改下划线的模板设置,以使用不同的符号来抵消插入的代码。定义interpolate 正则表达式匹配应逐字插入的表达式escape 正则表达式匹配在HTML转义后应插入的表达式,以及evaluate regex匹配应在不插入结果字符串的情况下计算的表达式。您可以定义或省略这三者的任意组合。例如,执行胡子。js样式模板:
_.templateSettings = { interpolate: /\\{\\{(.+?)\\}\\}/g };
var template = _.template("Hello {{ name }}!"); template({name: "Mustache"});
=>“你好,小胡子!”
如果我们退房/wp-includes/js/wp-util.js
我们看到这些正则表达式是如何在WordPress中定义的:
/**
* wp.template( id )
*
* Fetch a JavaScript template for an id, and return a templating function for it.
*
* @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
* For example, "attachment" maps to "tmpl-attachment".
* @return {function} A function that lazily-compiles the template requested.
*/
wp.template = _.memoize(function ( id ) {
var compiled,
/*
* Underscore\'s default ERB-style templates are incompatible with PHP
* when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
*
* @see trac ticket #22344.
*/
options = {
evaluate: /<#([\\s\\S]+?)#>/g,
interpolate: /\\{\\{\\{([\\s\\S]+?)\\}\\}\\}/g,
escape: /\\{\\{([^\\}]+?)\\}\\}(?!\\})/g,
variable: \'data\'
};
return function ( data ) {
compiled = compiled || _.template( $( \'#tmpl-\' + id ).html(), null, options );
return compiled( data );
};
});
请注意以下评论:
/*
* Underscore\'s default ERB-style templates are incompatible with PHP
* when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
*
这是相关的票
#22344 并对此进行讨论。