nakome
6/23/2017 - 12:34 AM

Micro templating

Micro templating

/**
* Inspired on absurd.js
* html <div id="app"></div>
* const t = new Micro({debug:false}); 
* debug true only show data in console
* you can use var|if|for|else|switch|case|break inside {% %}
* like {% if(this.show){ %} show {% } %}
* t.render({
*   el: '#app',
*   template: '<div>{%this.title%}</div>', 
*   data:{
*    title: 'Hello Wolrd'
*   }
* })
*/
'use strict';var _ck=function(){function a(b,c){for(var e,d=0;d<c.length;d++)e=c[d],e.enumerable=e.enumerable||!1,e.configurable=!0,'value'in e&&(e.writable=!0),Object.defineProperty(b,e.key,e)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();function _cck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}var Micro=function(){function a(b){_cck(this,a);this.options=b?b:{debug:!1}}return _ck(a,[{key:'q',value:function q(b){return document.querySelector(b)}},{key:'compile',value:function compile(b,c){for(var j,d=/{%(.+?)%}/g,e=/(^( )?(var|if|for|else|switch|case|break|{|}|;))(.*)?/g,f='with(obj) { var r=[];\n',g=0,k=function(l,m){return f+=m?l.match(e)?l+'\n':'r.push('+l+');\n':''==l?'':'r.push("'+l.replace(/"/g,'\\"')+'");\n',k};j=d.exec(b);)k(b.slice(g,j.index))(j[1],!0),g=j.index+j[0].length;k(b.substr(g,b.length-g)),f=(f+'return r.join(""); }').replace(/[\r\t\n]/g,' '),c||(c=[]);try{var h=new Function('obj',f).apply(c,[c])}catch(l){console.error(l.message)}return h}},{key:'render',value:function render(b){var c=this.compile(b.template,b.data);this.options.debug?(console.log(JSON.stringify(b.data,null,1)),console.log(c)):this.q(b.el).innerHTML=c}}]),a}()