很厉害的渲染模板

来源:互联网 发布:中国法律大全软件 编辑:程序博客网 时间:2024/04/26 19:40

$.fn.rendor2013=function(json_obj,obj_name,_html){
if(this[0] 
//&& this[0].className
){
var name_tpl = this[0].className;
if (!name_tpl){
_d_("target has no className");
return;
};


if(!window["_micro_templates_"]){
window["_micro_templates_"]={};
}
var _micro_templates_=window['_micro_templates_'];


if(!window["_micro_templates_s_"]){
window["_micro_templates_s_"]={};
}


//临时变量,用于compile出现问题时来得到临时返回字串...
var _micro_templates_s_=window['_micro_templates_s_'];


//如果未compile就compile一个...
var _func_tmp=function(){return arguments[0].replace(/'|\\/g, "\\$&").replace(/\n/g, "\\n");};//这个函数返回一个可以把单引号或者反斜杠全换成\$&,以及把真回车换成字符串\n
if (!_micro_templates_[name_tpl]){
var tpl=_html;
//tpl=tpl.replace(/<!--|&lt;!|&lt;%|\/\*/g, "<%");//处理左.. <!-- <! <% /*
//tpl=tpl.replace(/-->|!&gt;|%&gt;|\*\//g, "%>");//处理右.. --> !> %> */
///tpl=tpl.replace(/<!--|&lt;%/g, "<%");//处理左标签..形如 <!--  <% 
///tpl=tpl.replace(/-->|%&gt;/g, "%>");//处理右标签.. 形如 --> %>
////tpl=tpl.replace(/&lt;/g, "<");
////tpl=tpl.replace(/&gt;/g, ">");
tpl=tpl.replace(/<!--<%/g, "<%");//处理特殊左标签.. <!--<% 
tpl=tpl.replace(/%>-->/g, "%>");//处理特殊右标签.. %>-->
tpl=tpl.replace(/&lt;%/g, "<%");
tpl=tpl.replace(/%&gt;/g, "%>");
///tpl=tpl.replace(/&lt;%/g, "<%");
///tpl=tpl.replace(/%&gt;/g, "%>");
tpl=tpl.replace(/\r|\*+="/g, ' ');//把换行或者连续的空格变成单一空格..
tpl=tpl.split('<%').join("\r");//把左注释换成换行
tpl=tpl
.replace(/(?:^|%>)[^\r]*/g, _func_tmp)//不明觉厉..
.replace(/\r=(.*?)%>/g, "',$1,'")//不明觉厉..
.split("\r").join("');");//join回来..
tpl=tpl.split('%>').join("_write.push('");
try{
_micro_templates_s_[name_tpl]=tpl;
var _s="try{var _write=[];with("+obj_name+"){_write.push('"+ tpl +"');};return _write.join('');}catch(e){_d_('Syntax Error Template "+name_tpl+"'+window['_micro_templates_s_']['"+name_tpl+"']);}";
//if(false){//要看的话这个临时改true...
// _d_(_s);//看看编译后的模板..
//}
var nf= new Function(obj_name, _s);
}catch(e){
_d_("template compile error:\n"+_s+"\n\n"+window['_micro_templates_s_'][name_tpl]);throw e;
}
_micro_templates_[name_tpl] = nf;
}


//把compile好的提出..
var _nf=_micro_templates_[name_tpl];
//if (!json_obj) {
// return;
//};


//并运行它,把obj塞进去然后套现...
if(typeof(_nf)=="function"){
var _html=_nf(json_obj);
//if(true){//要看的话这个临时改false...
//_d_(_html);//看看渲染后的内容...
//console.error(_html);//看看渲染后的内容...
//}
this.html(_html);
}else{
_d_(name_tpl +" is not a function="+_nf);
}


//如果玩的是debug模式,把编译好的模板缓存给清掉...
if(window['debug'] && window['debug']>0){
_micro_templates_[name_tpl]=null;//no cache for debug mode
}


//这个搞什么JJ???
//arguments[1] || this.show();
}else{
_d_("this[0]?"+this[0].outerHTML);
//this.html("<b>Template not found</b>");
}
};
//渲染 模板..
//e.g. $("#target_id").rendorTpl(template_id, data, "_json");
$.fn.rendorTpl=function(tpl_id,obj,obj_name){
if(!obj_name) obj_name="_json";
try{
var _html=$("#"+tpl_id).html();//读出模板的内容..
//_d_(_html);
$(this).rendor2013(obj, obj_name, _html);//渲染
}catch(e){
_d_("tpl("+tpl_id+") throws e="+e);
}
};
/*
function my_rendor(tpl_id, target_id, data){
try{
var _html=$("#"+tpl_id).html();
$("#" + target_id+"").rendor2013(data, "_json", _html);
}catch(e){
_d_("error tpl "+tpl_id+" e="+e);
}
}
*/



原创粉丝点击