UNPKG

thunder

Version:

One of the fastest JavaScript template engine for Node.js and browsers.

18 lines (17 loc) 1.71 kB
/*! * thunder * Copyright(c) 2012 dreamerslab <ben@dreamerslab.com> * MIT Licensed * * @fileoverview * A lightning fast JavaScript template engine. */ (function(a){var b={}; var c={rules:{"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"},fn:function(e){return typeof(e)!="string"?e:e.replace(/[&<>"]/g,function(f){return c.rules[f]; });}};var d={version:"0.2.0",settings:{open:"<?",close:"?>"},compiled_text:function(f,h){f=f.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*/g,"\n");var e=(h&&h.compress===true?f.replace(/\r\n\t|\s+/g," "):f.replace(/\r\n|\r/g,"\n")).split(d.settings.open).join(d.settings.close+"\x1b").split(d.settings.close); var m="";var l=0;var g=e.length;var k="";for(;l<g;l++){k=e[l];m+=k.charAt(0)!="\x1b"?"_buf+='"+k.replace(/(\\|[\"\'])/g,"\\$1").replace(/\n/g,"\\n\\\n")+"'":(k.charAt(1)=="="?";_buf+="+k.substr(2).replace(/[\;\s]+$/,"")+";":(k.charAt(1)=="-"?";_buf+=e("+k.substr(2).replace(/[\;\s]+$/,"")+");":";"+k.substr(1).replace(/^\s+|\s+$/g,"").replace(/[\w\'\"\)\$]$/,"$&;"))); }return('var _buf="";'+m+";return _buf;").replace(/_buf\+\=\'\'\;/g,"").replace(/var _buf\=\"\"\;_buf\+\=/g,"var _buf=");},compile:function(e,f){var i=d.compiled_text(e,f); var g;try{g=new Function("it","e",i);}catch(h){console.log("[thunder] Having trouble with creating a template function: \n"+i);throw h;}return function(j){return g(j,c.fn); };},cached:function(e,f,g){g=g||e;if(!b[g]){b[g]=d.compile(e,f);}return b[g];},render:function(e,g,f){var h=f&&f.cached===true?"cached":"compile";return d[h](e,f)(g); },clear:function(){b={};}};if(typeof define!=="undefined"){return define(function(f,e,g){g.exports=d;});}if(typeof exports==="undefined"){a.thunder=d;return; }module.exports=d;})(this);