Icontem

File: templ.min.js

Recommend this page to a friend!
  Classes of Aliaksandr Astashenkau  >  Templ  >  templ.min.js  >  Download  
File: templ.min.js
Role: Class source
Content type: text/plain
Description: Minified
Class: Templ
Processes templates embedded in the pages
Author: By
Last change: Bug fix
Date: 8 years ago
Size: 3,144 bytes
 

Contents

Class file image Download
/**
 * Copyright (c) 2011 Aliaksandr Astashenkau
 * @author-website http://dfsq.info
 * @license MIT License
 */
(function(){this.Templ=function(tmplStr,tmplData){var id=1,tokens={},proccessMarkers=function(a){var b=0;while(b<a.length){if(a.charAt(b)+a.charAt(b+1)=="{{"){var c=getId(),d=a.indexOf("}}",b),e=a.slice(b+2,d).replace(/(^\s*)|(\s*$)/g,"").split("|");tokens[c]={buffer:e.shift(),modif:e},a=replaceWith(a,"{__"+c+"}",b,d+2)}b++}return a},proccessControls=function(a,b,c,d){var e=b;while(b<a.length){if(a.charAt(b)+a.charAt(b+1)=="{%"){var f=a.slice(b+2,a.indexOf("%}",b));if(c&&f.match(c)){var g=getId(),h=e-2,i=b+f.length+4;tokens[g]={buffer:a.slice(h,i),expr:d};return replaceWith(a,"{__"+g+"}",h,i)}var j=f.match(/\s*for\s+((?:\w+\s*,)?\s*\w+)\s+in\s+([\w.]+)\s*/i);if(j)a=proccessControls(a,b+2,/\s*endfor\s*/i,{type:"for",elem:j[1],list:j[2]});else{var k=f.match(/\s*if\s+(.+)\s*/i);k&&(a=proccessControls(a,b+2,/\s*endif\s*/i,{type:"if",cond:k[1]}))}}b++}return a},parse=function(str,data){str=str.replace(/{__(\d+?)}/g,function(a,b){var token=tokens[b];if(!token.expr){var repl=evl(data,tokens[b].buffer,'TEMPL ERROR: Undefined variable "'+tokens[b].buffer+'".');if(token.modif.length)for(var i in token.modif){var modif=token.modif[i],params=[],check=token.modif[i].match(/(\w+)\(([\s\S]+)\)/);if(check){modif=check[1],params=check[2].split(/\s*,\s*/);with(data)for(var j in params)params[j]=eval(params[j])}params.unshift(repl),modif=Templ.modifiers[modif]||window[modif];if(typeof modif!="function")throw new Error('TEMPL ERROR: Unknown modifier "'+token.modif[i]+'".');repl=modif.apply(this,params)}return repl}switch(token.expr.type){case"if":var cond=evl(data,token.expr.cond,"TEMPL ERROR: invalid condition: "+token.expr.cond+"."),block=token.buffer.match(cond?/{%\s*if\s+.+?\s*%}([\s\S]*?){%/i:/{%\s*else\s*%}([\s\S]*?){%/i);return block?parse(block[1],data):"";case"for":with(data)var loopData=eval(token.expr.list);if(typeof loopData=="undefined")throw new Error('TEMPL ERROR: Undefined list "'+token.expr.list+'".');if(hasElements(loopData)){var block=token.buffer.match(/{%\s*for.*?\s*%}([\s\S]*?){%/i);if(block){var key,elem=token.expr.elem,split=elem.split(/\s*,\s*/);split.length==2&&(key=split[0],elem=split[1]);var subStr="";for(var k in loopData){var tmpObj={};key&&(tmpObj[key]=k),tmpObj[elem]=loopData[k],subStr+=parse(block[1],tmpObj)}return subStr}return""}var block=token.buffer.match(/{%\s*else\s*%}([\s\S]*?){%/i);return block?parse(block[1],loopData):""}});return str},tokenize=function(a){return proccessControls(proccessMarkers(a),0)},evl=function(data,buffer,err){var ret;with(data)try{ret=eval(buffer)}catch(e){try{with(tmplData)ret=eval(buffer)}catch(e){throw new Error(err)}}return ret},getId=function(){return id++},replaceWith=function(a,b,c,d){return a.substr(0,c)+b+a.substr(d)},hasElements=function(a){if(a.hasOwnProperty("length"))return Boolean(a.length);for(var b in a)if(a.hasOwnProperty(b))return!0;return!1},init=function(){return parse(tokenize(tmplStr),tmplData)};return init()},this.Templ.modifiers={},this.Templ.addModifiers=function(a){for(var b in a)this.modifiers[b]=a[b]}})()