forked from goofychris/art-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.min.js
1 lines (1 loc) · 4.32 KB
/
template.min.js
1
/*!artTemplate - Template Engine*/var template=function(e,t){return template[typeof t=="object"?"render":"compile"].apply(template,arguments)};(function(e,t){"use strict";e.version="2.0.0",e.openTag="<%",e.closeTag="%>",e.isEscape=!0,e.isCompress=!1,e.parser=null,e.render=function(e,t){var n=r(e);return n===undefined?i({id:e,name:"Render Error",message:"No Template"}):n(t)},e.compile=function(t,r){function c(n){try{return new f(n)+""}catch(s){return u?(s.id=t||r,s.name="Render Error",s.source=r,i(s)):e.compile(t,r,!0)(n)}}var o=arguments,u=o[2],a="anonymous";typeof r!="string"&&(u=o[1],r=o[0],t=a);try{var f=s(r,u)}catch(l){return l.id=t||r,l.name="Syntax Error",i(l)}return c.prototype=f.prototype,c.toString=function(){return f.toString()},t!==a&&(n[t]=c),c},e.helper=function(t,n){e.prototype[t]=n},e.onerror=function(e){var n="[template]:\n"+e.id+"\n\n[name]:\n"+e.name;e.message&&(n+="\n\n[message]:\n"+e.message),e.line&&(n+="\n\n[line]:\n"+e.line,n+="\n\n[source]:\n"+e.source.split(/\n/)[e.line-1].replace(/^[\s\t]+/,"")),e.temp&&(n+="\n\n[temp]:\n"+e.temp),t.console&&console.error(n)};var n={},r=function(r){var i=n[r];if(i===undefined&&"document"in t){var s=document.getElementById(r);if(s){var o=s.value||s.innerHTML;return e.compile(r,o.replace(/^\s*|\s*$/g,""))}}else if(n.hasOwnProperty(r))return i},i=function(t){function n(){return n+""}return e.onerror(t),n.toString=function(){return"{Template Error}"},n},s=function(){e.prototype={$render:e.render,$escape:function(e){return typeof e=="string"?e.replace(/&(?![\w#]+;)|[<>"']/g,function(e){return{"<":"<",">":">",'"':""","'":"'","&":"&"}[e]}):e},$string:function(e){return typeof e=="string"||typeof e=="number"?e:typeof e=="function"?e():""}};var t=Array.prototype.forEach||function(e,t){var n=this.length>>>0;for(var r=0;r<n;r++)r in this&&e.call(t,this[r],r,this)},n=function(e,n){t.call(e,n)},r="break,case,catch,continue,debugger,default,delete,do,else,false,finally,for,function,if,in,instanceof,new,null,return,switch,this,throw,true,try,typeof,var,void,while,with,abstract,boolean,byte,char,class,const,double,enum,export,extends,final,float,goto,implements,import,int,interface,long,native,package,private,protected,public,short,static,super,synchronized,throws,transient,volatile,arguments,let,yield,undefined",i=new RegExp(["/\\*(.|\n)*?\\*/|//[^\n]*\n|//[^\n]*$","'[^']*'|\"[^\"]*\"","\\.[s \n]*[\\$\\w]+","\\b"+r.replace(/,/g,"\\b|\\b")+"\\b"].join("|"),"g"),s=function(e){return e=e.replace(i,",").replace(/[^\w\$]+/g,",").replace(/^,|^\d+|,\d+|,$/g,""),e?e.split(","):[]};return function(t,r){function S(t){return l+=t.split(/\n/).length-1,e.isCompress&&(t=t.replace(/[\n\r\t\s]+/g," ")),t=t.replace(/('|\\)/g,"\\$1").replace(/\r/g,"\\r").replace(/\n/g,"\\n"),t=m[1]+"'"+t+"'"+m[2],t+"\n"}function x(t){var n=l;u?t=u(t):r&&(t=t.replace(/\n/g,function(){return l++,"$line="+l+";"}));if(t.indexOf("=")===0){var i=t.indexOf("==")!==0;t=t.replace(/^=*|[\s;]*$/g,"");if(i&&e.isEscape){var s=t.replace(/\s*\([^\)]+\)/,"");!h.hasOwnProperty(s)&&!/^(include|print)$/.test(s)&&(t="$escape($string("+t+"))")}else t="$string("+t+")";t=m[1]+t+m[2]}return r&&(t="$line="+n+";"+t),T(t),t+"\n"}function T(e){e=s(e),n(e,function(e){c.hasOwnProperty(e)||(N(e),c[e]=!0)})}function N(e){var t;e==="print"?t=y:e==="include"?(p.$render=h.$render,t=b):(t="$data."+e,h.hasOwnProperty(e)&&(p[e]=h[e],e.indexOf("$")===0?t="$helpers."+e:t=t+"===undefined?$helpers."+e+":"+t)),d+=e+"="+t+","}var i=e.openTag,o=e.closeTag,u=e.parser,a=t,f="",l=1,c={$data:!0,$helpers:!0,$out:!0,$line:!0},h=e.prototype,p={},d="var $helpers=this,"+(r?"$line=0,":""),v="".trim,m=v?["$out='';","$out+=",";","$out"]:["$out=[];","$out.push(",");","$out.join('')"],g=v?"if(content!==undefined){$out+=content;return content}":"$out.push(content);",y="function(content){"+g+"}",b="function(id,data){if(data===undefined){data=$data}var content=$helpers.$render(id,data);"+g+"}";n(a.split(i),function(e,t){e=e.split(o);var n=e[0],r=e[1];e.length===1?f+=S(n):(f+=x(n),r&&(f+=S(r)))}),a=f,r&&(a="try{"+a+"}catch(e){"+"e.line=$line;"+"throw e"+"}"),a="'use strict';"+d+m[0]+a+"return new String("+m[3]+")";try{var w=new Function("$data",a);return w.prototype=p,w}catch(E){throw E.temp="function anonymous($data) {"+a+"}",E}}}()})(template,this),typeof define=="function"?define(function(e,t,n){n.exports=template}):typeof exports!="undefined"&&(module.exports=template)