exports.compile = function compile(template, opts) {
var templ;
// v1 compat
// 'scope' is 'context'
// FIXME: Remove this in a future version
//模板 参数
if (opts && opts.scope) {
if (!scopeOptionWarned){
console.warn('`scope` option is deprecated and will be removed in EJS 3');
scopeOptionWarned = true;
}
if (!opts.context) {
opts.context = opts.scope;
}
delete opts.scope;
}
templ = new Template(template, opts);
return templ.compile();
};
Template.prototype = {
createRegex: function () {
//创建的一些正则分组
var str = _REGEX_STRING;
//得到分隔符
var delim = utils.escapeRegExpChars(this.opts.delimiter);
//分到分隔符的首字母
var open = utils.escapeRegExpChars(this.opts.openDelimiter);
//分到分隔符的结束字母
var close = utils.escapeRegExpChars(this.opts.closeDelimiter);
//根据分隔符,重新处理正则表达式
str = str.replace(/%/g, delim)
.replace(/</g, open)
.replace(/>/g, close);
return new RegExp(str);
},
//解析模板的数据
parseTemplateText: function () {
var str = this.templateText;
var pat = this.regex;
var result = pat.exec(str);
var arr = [];
var firstPos;
while (result) {
firstPos = result.index;
if (firstPos !== 0) {
//取标签
arr.push(str.substring(0, firstPos));
str = str.slice(firstPos);
}
arr.push(result[0]);
str = str.slice(result[0].length);
result = pat.exec(str);
}
if (str) {
arr.push(str);
}
return arr;
},
后续再看