(function($){
$.template=function(html,options){
return new $.template.instance(html,options);
};
$.template.instance=function(html,options){
if(options&&options['regx'])options.regx=this.regx[options.regx];
this.options=$.extend({
compile:false,
regx:this.regx.standard
},options||{});
this.html=html;
if(this.options.compile){
this.compile();
}
this.isTemplate=true;
};
$.template.regx=$.template.instance.prototype.regx={
jsp:/\$\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
ext:/\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
jtemplates:/\{\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}\}/g
};
$.template.regx.standard=$.template.regx.jsp;
$.template.helpers=$.template.instance.prototype.helpers={
substr:function(value,start,length){
return String(value).substr(start,length);
}
};
$.extend($.template.instance.prototype,{
apply:function(values){
if(this.options.compile){
return this.compiled(values);
}else{
var tpl=this;
var fm=this.helpers;
var fn=function(m,name,format,args){
if(format){
if(format.substr(0,5)=="this."){
return tpl.call(format.substr(5),values[name],values);
}else{
if(args){
var re=/^\s*['"](.*)["']\s*$/;
args=args.split(',');
for(var i=0,len=args.length;i<len;i++){
args[i]=args[i].replace(re,"$1");
}
args=[values[name]].concat(args);
}else{
args=[values[name]];
}
return fm[format].apply(fm,args);
}
}else{
return values[name]!==undefined?values[name]:"";
}
};
return this.html.replace(this.options.regx,fn);
}
},
compile:function(){
var sep=$.browser.mozilla?"+":",";
var fm=this.helpers;
var fn=function(m,name,format,args){
if(format){
args=args?','+args:"";
if(format.substr(0,5)!="this."){
format="fm."+format+'(';
}else{
format='this.call("'+format.substr(5)+'", ';
args=", values";
}
}else{
args='';format="(values['"+name+"'] == undefined ? '' : ";
}
return"'"+sep+format+"values['"+name+"']"+args+")"+sep+"'";
};
var body;
if($.browser.mozilla){
body="this.compiled = function(values){ return '"+
this.html.replace(/\\/g,'\\\\').replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.options.regx,fn)+
"';};";
}else{
body=["this.compiled = function(values){ return ['"];
body.push(this.html.replace(/\\/g,'\\\\').replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.options.regx,fn));
body.push("'].join('');};");
body=body.join('');
}
eval(body);
return this;
}
});
var $_old={
domManip:$.fn.domManip,
text:$.fn.text,
html:$.fn.html
};
$.fn.domManip=function(args,table,reverse,callback){
if(args[0].isTemplate){
args[0]=args[0].apply(args[1]);
delete args[1];
}
var r=$_old.domManip.apply(this,arguments);
return r;
};
$.fn.html=function(value,o){
if(value&&value.isTemplate)var value=value.apply(o);
var r=$_old.html.apply(this,[value]);
return r;
};
$.fn.text=function(value,o){
if(value&&value.isTemplate)var value=value.apply(o);
var r=$_old.text.apply(this,[value]);
return r;
};
})(jQuery);
