image.png

    1. exports.compile = function compile(template, opts) {
    2. var templ;
    3. // v1 compat
    4. // 'scope' is 'context'
    5. // FIXME: Remove this in a future version
    6. //模板 参数
    7. if (opts && opts.scope) {
    8. if (!scopeOptionWarned){
    9. console.warn('`scope` option is deprecated and will be removed in EJS 3');
    10. scopeOptionWarned = true;
    11. }
    12. if (!opts.context) {
    13. opts.context = opts.scope;
    14. }
    15. delete opts.scope;
    16. }
    17. templ = new Template(template, opts);
    18. return templ.compile();
    19. };

    image.png

    1. Template.prototype = {
    2. createRegex: function () {
    3. //创建的一些正则分组
    4. var str = _REGEX_STRING;
    5. //得到分隔符
    6. var delim = utils.escapeRegExpChars(this.opts.delimiter);
    7. //分到分隔符的首字母
    8. var open = utils.escapeRegExpChars(this.opts.openDelimiter);
    9. //分到分隔符的结束字母
    10. var close = utils.escapeRegExpChars(this.opts.closeDelimiter);
    11. //根据分隔符,重新处理正则表达式
    12. str = str.replace(/%/g, delim)
    13. .replace(/</g, open)
    14. .replace(/>/g, close);
    15. return new RegExp(str);
    16. },

    image.png

    image.png

    1. //解析模板的数据
    2. parseTemplateText: function () {
    3. var str = this.templateText;
    4. var pat = this.regex;
    5. var result = pat.exec(str);
    6. var arr = [];
    7. var firstPos;
    8. while (result) {
    9. firstPos = result.index;
    10. if (firstPos !== 0) {
    11. //取标签
    12. arr.push(str.substring(0, firstPos));
    13. str = str.slice(firstPos);
    14. }
    15. arr.push(result[0]);
    16. str = str.slice(result[0].length);
    17. result = pat.exec(str);
    18. }
    19. if (str) {
    20. arr.push(str);
    21. }
    22. return arr;
    23. },

    image.png
    后续再看