tmpl.js 564 B

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