SOURCE

function Template(tpl) {
    const regex = /\{\s*([a-zA-Z\.\_0-9()]+)\s*\}/m;
  let match;

  
  var code = ['var r=[];'];
  
  var addLine = function (text) {
    code.push('r.push(\'' +
            text.replace(/\'/g, '\\\'')
                .replace(/\n/g, '\\n')
                .replace(/\r/g, '\\r')
            + '\');');
  };

  while (match = regex.exec(tpl)) {
    if (match.index > 0) {
      addLine(tpl.slice(0, match.index));
    }
    code.push('r.push(this.' + match[1] + ');');
    tpl = tpl.substring(match.index + match[0].length);
  }
  addLine(tpl);
  code.push('return r.join(\'\');');
  // 创建函数
  const fn = new Function(code.join('\n'));
  // 用 render() 调用函数并绑定 this 参数:
  this.render = function (model) {
    return fn.apply(model);
  };
}

var tpl = new Template('<p>Today: { date }</p>\n<a href="/{ user.id }">{ user.company }</a><span>1111</span>');

var model = {
    date: 20150316,
    user: {
        id: 'A-000&001',
        company: 'AT&T'
    }
};
var html = tpl.render(model);
console.log('html',html);
console 命令行工具 X clear

                    
>
console