Skip to content

Instantly share code, notes, and snippets.

@LinZap
Created October 27, 2015 21:20
Show Gist options
  • Save LinZap/c14852df34a41184de08 to your computer and use it in GitHub Desktop.
Save LinZap/c14852df34a41184de08 to your computer and use it in GitHub Desktop.
EJS by Zap
/*
I3S-Awesome V3.0
Render System
@Zap
*/
(function(){
UI = {};
Render = function(option){ new R(option); }
var Compiler = function(str,data){
str = str.replace(/[\r\n\t]/g,'');
var events_pool = [],
match,
chunks = [],
delimiter = /(<%=)|(<%)|(%>)|({{)|(}})/i;
while(match = delimiter.exec(str)){
var prefix = str.slice(0,match.index),
suffix = str.slice(match.index);
chunks.push(prefix);
chunks.push(match[0]);
str = suffix.replace(delimiter,'');
}
if(str!="") chunks.push(str);
var fun_tostr = "function tostr(item){if(item) if(item.toString) return item.toString(); }",
evalstr = "(function(d){"+fun_tostr+" with(d){var _res='';",
tmpstr = "",
f_op = false,
f_js = false,
f_in = false;
chunks.forEach(function(chunk,i){
switch(chunk) {
case "<%=": flush();f_op = true; break;
case '<%': flush();f_js = true; break;
case '{{': flush(); f_in = true; break;
case '}}':
case '%>': flush(); break;
default: tmpstr+= chunk; break;
}
});
if(tmpstr!="") flush();
evalstr+= "return _res;}})("+JSON.stringify(data)+");";
function flush(){
if(tmpstr=='') return;
if(f_op){
evalstr += "_res+=tostr("+tmpstr+");";
f_op = false;
}
else if(f_js){
evalstr += tmpstr;
f_js = false;
}
else if(f_in){
var kv = tmpstr.split("=");
if(kv.length>1){
var events = " ejs-"+kv[0]+"="+kv[1];
evalstr+="_res+=tostr('"+events+"');";
events_pool.push(kv[0]);
}
f_in = false;
}
else{
evalstr += "_res+=tostr('"+tmpstr+"');";
}
tmpstr = "";
}
return {
str: eval(evalstr),
evt: events_pool
};
};
var R = function(option){
option.subversion = Math.floor((Math.random()*1000000));
option.data = option.data || {};
if(option.ejs){
if(option.ejs.path){
$.ajax({
url: 'ejs/'+option.ejs.path+'.ejs',
dataType: 'text',
data: {v: option.subversion},
})
.done(function(str) {VDOM(str,option);})
.fail(function() { console.log("fail:"+option.ejs.path); });
}
else if(option.ejs.txt){
VDOM(option.ejs.txt,option);
}
}
else{ return null; }
}
var VDOM = function(str,option){
var res = Compiler(str,option.data),
ele = $('<div>').html(res.str).children();
res.evt.forEach(function(evt){
ele.find('[ejs-'+evt+']').each(function(){bindEventHandler.call(this,evt)});
ele.filter('[ejs-'+evt+']').each(function(){bindEventHandler.call(this,evt)});
});
function bindEventHandler(evt){
var funName = $(this).attr('ejs-'+evt);
$.extend(this, option);
if(option[funName]) $(this).on(evt,function(){
var t = $(this); $.extend(t, option);
option[funName].call(t);
});
}
if(option.target){
if(option.append) option.target.append(ele);
else if(option.prepend) option.target.prepend(ele);
else option.target.html(ele);
}
if(option.callback) {
$.extend(ele,option);
option.callback.call(ele);
}
ele.find('[ejs]').each(quickRender);
ele.filter('[ejs]').each(quickRender);
function quickRender(){
var el = $(this),
ejs_fun = el.attr('ejs'),
funName = ejs_fun.slice(1);
if(ejs_fun.indexOf("@")==0){
if(UI[funName]) UI[funName].call(this,el);
}
else Render({ ejs: { path: ejs_fun }, target: el });
}
}
})();
@LinZap
Copy link
Author

LinZap commented Oct 27, 2015

Render({
ejs:{ path:'post' },
data: {item:data},
target: $('#section')
});

@LinZap
Copy link
Author

LinZap commented Oct 27, 2015

Render({

ejs:{ txt:ejs },
data: data,
target: $('#box'),

callback: function(ele){
console.log("CALLBACK",this,ele);
},
onClickHandler: function(){
this.text(this.getRandomInt());
},
getRandomInt: function(){
return Math.floor(Math.random() * 1000000);
}

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment