tmpl.js
1 var INTERPOLATE = /{([^{]+?)}/g 2 3 module.exports = function(str, data) { 4 var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + 5 'with(obj||{}){__p.push(\'' + 6 str.replace(/\\/g, '\\\\') 7 .replace(/'/g, "\\'") 8 .replace(INTERPOLATE, function(match, code) { 9 return "'," + code.replace(/\\'/g, "'") + ",'" 10 }) 11 .replace(/\r/g, '\\r') 12 .replace(/\n/g, '\\n') 13 .replace(/\t/g, '\\t') 14 + "');}return __p.join('');" 15 var func = new Function('obj', tmpl) 16 return data ? func(data) : func 17 }