forked from wentout/template_render_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tre.min.js
6 lines (6 loc) · 2.31 KB
/
tre.min.js
1
2
3
4
5
6
/* The MIT License (MIT) Copyright (c) 2013 went.out */
var render=function(){var p={upper:function(b){return b.toUpperCase()},lower:function(b){return b.toLowerCase()},escape:function(b){return b.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")},trim:function(){return String.prototype.trim?function(b){return b.trim()}:function(b){return b.replace(/^\s+|\s+$/g,"")}}(),capitalize:function(b){b=b.split(" ");for(var d=b.length,k=[],f=0;f<d;f++){var g=b[f];g[0]?k.push(g[0].toUpperCase()+g.slice(1)):
k.push("")}return k.join(" ")}},r=function(b,d){for(var k=d.split("."),f=k.length,g,l=0,a=0;a<f;a++){var e=k[a];g?g[e]&&(g=g[e],l++):b[e]&&(g=b[e],l++)}if(l==f)return g},t=function(b,d,k){var f="",g=/\{\{\s*.+?\s*\}\}/g,l=b.split(g);b=b.match(g);f+=l[0];if(b){for(var g=b.length,a=0;a<g;a++){var e=b[a],n=e.replace(/^\{\{\s*|\s*\}\}$/g,"").split(":"),c=n[0],h="";"."==c[0]?h=1<c.length?h+r(d,c.slice(1)):h+d:(c=r(d,c),"function"==typeof c?h+=c(k,d):c&&(h+=c));""==h&&-1<e.indexOf("default")&&(h=e.replace(/^[^]*\(\s*|\s*\)[^]*$/g,
""));e=n.length;if(1<e)for(c=1;c<e;c++){var m=n[c];p[m]&&(h=p[m](h))}f+=h}0<g&&(f+=l[g])}return f},q=function(b,d,k,f){for(var g=b.length,l=0;l<g;l++){var a=b[l];if(a.cnt){var e=a.cnt;a.sub&&(e=k||a.cnt);switch(Object.prototype.toString.call(e)){case "[object Array]":for(var n=e.length,c=0;c<n;c++)d.push([a.be4e,e[c]]),q(a.nest,d,e[c],f);break;case "[object Object]":for(c in e)e.hasOwnProperty(c)&&(d.push([a.be4e,e[c]]),q(a.nest,d,e[c],f));break;default:if("function"==typeof e){d.push([a.be4e,h]);
var h=e(a,f,b,d,k);void 0!==h&&q(a.nest,d,h,f)}else d.push([a.be4e,e]),q(a.nest,d,void 0,f)}d.push([a.af3e.str,a.af3e.cnt])}}};return function(b,d){b=b.replace(/\{#[^]*?#\}/g,"");var k=/\{\%\s*[a-zA-Z0-9._/:-]+?\s*\%\}/g,f=b.split(k),k=b.match(k),g=[[f[0],d]],l=[],a=[],e=[];if(k){for(var n=k.length,c=0;c<n;c++){var h=k[c].replace(/^\{\%\s*|\s*\%\}$/g,""),m=a.length;if("/"===h)a[m-1].af3e={cnt:a[m-2]?a[m-2].cnt:d,str:f[c+1]},a.pop();else{var s=d,p=!1;"."===h?(p=!0,s=0<m?a[m-1].cnt:d):s=r(d,h);h={cnt:s,
sub:p,nest:[],be4e:f[c+1],af3e:{cnt:null,str:""}};0==m?(l.push(h),a.push(h)):(a[m-1].nest.push(h),a.push(a[m-1].nest[a[m-1].nest.length-1]))}}q(l,g,void 0,d);n=g.length;for(c=0;c<n;c++)e.push(t(g[c][0],g[c][1],d))}else e.push(t(b,d,d));return e.join("")}}();