tmpl.js 622 B

12345678910111213141516171819202122232425
  1. (function(){
  2. var cache = {};
  3. this.tmpl = function (str, data){
  4. var fn = !/\W/.test(str) ?
  5. cache[str] = cache[str] ||
  6. tmpl(document.getElementById(str).innerHTML) :
  7. new Function("obj",
  8. "var p=[],print=function(){p.push.apply(p,arguments);};" +
  9. "with(obj){p.push('" +
  10. str
  11. .replace(/[\r\t\n]/g, " ")
  12. .split("<%").join("\t")
  13. .replace(/((^|%>)[^\t]*)'/g, "$1\r")
  14. .replace(/\t=(.*?)%>/g, "',$1,'")
  15. .split("\t").join("');")
  16. .split("%>").join("p.push('")
  17. .split("\r").join("\\'")
  18. + "');}return p.join('');");
  19. return data ? fn( data ) : fn;
  20. };
  21. })();