var TemplateEngine = function(html, options) {
    var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0;
    var add = function(line, js) {
        js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
            (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
        return add;
    }
    while(match = re.exec(html)) {
        add(html.slice(cursor, match.index))(match[1], true);
        cursor = match.index + match[0].length;
    }
    add(html.substr(cursor, html.length - cursor));
    code += 'return r.join("");';
    return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
}</%([^%>

 

相关文章:

  • 2022-01-02
  • 2021-12-03
  • 2022-12-23
  • 2021-12-31
  • 2021-10-22
  • 2021-12-21
猜你喜欢
  • 2021-12-19
  • 2021-11-04
  • 2022-12-23
  • 2021-11-07
  • 2021-09-21
  • 2021-10-07
  • 2021-12-22
相关资源
相似解决方案