Javascript模板
/***Template.class.js***/
functionTemplate()
{
this.classname="Template";
this.debug=false;
this.file=newHashMap();
this.root="";
this.varkeys=newHashMap();
this.varvals=newHashMap();
this.unknowns="remove";
this.halt_on_error="yes";
this.last_error="";
this.fso=newActiveXObject("Scripting.FileSystemObject");
this.set_root=_set_root;
this.set_unknowns=_set_unknowns;
this.get_var=_get_var;
this.set_file=_set_file;
this.set_var=_set_var;
this.set_block=_set_block;
this.subst=_subst;
this.parse=_parse;
this.p=_p;
this.pparse=_pparse;
this.finish=_finish;
this.loadfile=_loadfile;
this.is_dir=_is_dir;
this.file_exists=_file_exists;
this.filename=_filename;
this.varname=_varname;
this.halt=_halt;
this.haltmsg=_haltmsg;
}
/**
*設(shè)置模板文件根目錄
*@paramroot
*/
function_set_root(root)
{
if(!this.is_dir(root))
{
this.halt("set_root:"+root+"isnotadirectory.");
}
this.root=root;
}
/**
*設(shè)定對(duì)未知模板變量的處理辦法
*@paramunknowns
*/
function_set_unknowns(unknowns)
{
this.unknowns=unknowns;
}
/**
*設(shè)定模板文件
*@paramhandle
*@paramfilename
*/
function_set_file(handle,filename)
{
this.file.put(handle,this.filename(filename));
}
/**
*設(shè)定模板變量
*@paramvarname
*@paramvalue
*/
function_set_var(varname,value)
{
if(!this.varkeys.containsKey(varname))
{
this.varkeys.put(varname,this.varname(varname));
}
if(!this.varvals.containsKey(varname))
{
this.varvals.put(varname,value);
}
else
{
this.varvals.remove(varname);
this.varvals.put(varname,value);
}
//alert(varname+"=================="+value);
}
/**
*設(shè)定塊變量
*@paramparent
*@paramhandle
*@paramname
*/
function_set_block(parent,handle,name)
{
if(!this.loadfile(parent))
{
this.halt("subst:unabletoload"+parent);
}
if(name=="")
{
name=handle;
}
varstr=this.get_var(parent);
varre=newRegExp("([sS.]*)");
//Matcherm=p.matcher(str);
//varrs=m.find();
//vart=m.group(m.groupCount());
//this.set_var(handle,t);
vararr=re.exec(str);
if(arr!=null)
this.set_var(handle,RegExp.$1);
str=str.replace(re,"{"+name+"}");
this.set_var(parent,str);
}
/**
*進(jìn)行變量替換
*@paramhandle
*@return
*/
function_subst(handle)
{
if(!this.loadfile(handle))
{
this.halt("subst:unabletoload"+handle);
}
varstr=this.get_var(handle);
varkeys=this.varkeys.keySet();
//alert(keys.length);
for(vari=0;i<keys.length;i++)
{
varkey=keys[i];
varre=newRegExp(this.varkeys.get(key),"g")
str=str.replace(re,this.varvals.get(key));
}
//alert(handle+"++++++++++++++++++"+str);
returnstr;
}
/**
*進(jìn)行變量復(fù)制
*@paramtarget
*@paramhandle
*@paramappend
*/
function_parse(target,handle,append)
{
varstr=this.subst(handle);
if(append)
{
this.set_var(target,this.get_var(target)+str);
}
else
{
this.set_var(target,str);
}
}
/**
*返回替換后的文件
*@paramvarname
*@return
*/
function_p(varname)
{
returnthis.finish(this.get_var(varname));
}
/**
*parse()和p()的合并
*@paramtarget
*@paramhandle
*@paramappend
*@return
*/
function_pparse(target,handle,append)
{
this.parse(target,handle,append);
document.writeln(this.p(target));
}
/**
*加載模板文件
*@paramhandle
*@return
*/
function_loadfile(handle)
{
if(this.varkeys.containsKey(handle)&&this.varvals.get(handle)!=null)
{
returntrue;
}
if(!this.file.containsKey(handle))
{
_halt("loadfile:"+handle+"isnotavalidhandle.");
returnfalse;
}
varfilename=this.file.get(handle);
if(!this.file_exists(filename))
{
this.halt("loadfile:whileloading"+handle+","+filename+"doesnotexist.");
returnfalse;
}
try
{
varfr=this.fso.OpenTextFile(filename);
vars=fr.ReadAll();
if(s=="")
{
halt("loadfile:whileloading"+handle+","+filename+"isempty.");
returnfalse;
}
this.set_var(handle,s);
}
catch(e)
{
}
returntrue;
}
/**
*獲取變量
*@paramvarname
*@return
*/
function_get_var(varname)
{
if(this.varvals.containsKey(varname))
returnthis.varvals.get(varname);
else
return"";
}
/**
*判斷目錄
*@parampath
*@return
*/
function_is_dir(path)
{
if(this.fso.FolderExists(path))
returntrue;
else
returnfalse;
}
/**
*判斷文件
*@paramfilename
*@return
*/
function_file_exists(filename)
{
if(this.fso.FileExists(filename))
returntrue;
else
returnfalse;
}
/**
*文件名處理
*@paramfilename
*@return
*/
function_filename(filename)
{
if(!this.file_exists(this.root+filename))
{
this.halt("filename:file"+filename+"doesnotexist.");
}
returnthis.root+filename;
}
/**
*變量名處理
*@paramvarname
*@return
*/
function_varname(varname)
{
return"{"+varname+"}";
}
/**
*完成字符串的處理
*@paramstr
*@return
*/
function_finish(str)
{
varre=newRegExp("{[^trn}]+}","g");
if(this.unknowns=="remove")
{
str=str.replace(re,"");
}
elseif(this.unknowns=="comment")
{
str=str.replace(re,"");
}
else
{
}
returnstr;
}
function_halt(msg)
{
this.last_error=msg;
if(this.halt_on_error!="no")
{
_haltmsg(msg);
}
if(this.halt_on_error=="yes")
{
alert("Halted.");
//System.exit(0);
}
}
function_haltmsg(msg)
{
alert("TemplateError:"+msg);
}
/**
*HashMap構(gòu)造函數(shù)
*/
functionHashMap()
{
this.length=0;
this.prefix="hashmap_prefix_20050524_";
}
/**
*向HashMap中添加鍵值對(duì)
*/
HashMap.prototype.put=function(key,value)
{
this[this.prefix+key]=value;
this.length++;
}
/**
*從HashMap中獲取value值
*/
HashMap.prototype.get=function(key)
{
returntypeofthis[this.prefix+key]=="undefined"
?null:this[this.prefix+key];
}
/**
*從HashMap中獲取所有key的集合,以數(shù)組形式返回
*/
HashMap.prototype.keySet=function()
{
vararrKeySet=newArray();
varindex=0;
for(varstrKeyinthis)
{
if(strKey.substring(0,this.prefix.length)==this.prefix)
arrKeySet[index++]=strKey.substring(this.prefix.length);
}
returnarrKeySet.length==0?null:arrKeySet;
}
/**
*從HashMap中獲取value的集合,以數(shù)組形式返回
*/
HashMap.prototype.values=function()
{
vararrValues=newArray();
varindex=0;
for(varstrKeyinthis)
{
if(strKey.substring(0,this.prefix.length)==this.prefix)
arrValues[index++]=this[strKey];
}
returnarrValues.length==0?null:arrValues;
}
/**
*獲取HashMap的value值數(shù)量
*/
HashMap.prototype.size=function()
{
returnthis.length;
}
/**
*刪除指定的值
*/
HashMap.prototype.remove=function(key)
{
this[this.prefix+key];
this.length--;
}
/**
*清空HashMap
*/
HashMap.prototype.clear=function()
{
for(varstrKeyinthis)
{
if(strKey.substring(0,this.prefix.length)==this.prefix)
this[strKey];
}
this.length=0;
}
/**
*判斷HashMap是否為空
*/
HashMap.prototype.isEmpty=function()
{
returnthis.length==0;
}
/**
*判斷HashMap是否存在某個(gè)key
*/
HashMap.prototype.containsKey=function(key)
{
for(varstrKeyinthis)
{
if(strKey==this.prefix+key)
returntrue;
}
returnfalse;
}
/**
*判斷HashMap是否存在某個(gè)value
*/
HashMap.prototype.containsValue=function(value)
{
for(varstrKeyinthis)
{
if(this[strKey]==value)
returntrue;
}
returnfalse;
}
/**
*把一個(gè)HashMap的值加入到另一個(gè)HashMap中,參數(shù)必須是HashMap
*/
HashMap.prototype.putAll=function(map)
{
if(map==null)
return;
if(map.constructor!=JHashMap)
return;
vararrKey=map.keySet();
vararrValue=map.values();
for(variinarrKey)
this.put(arrKey[i],arrValue[i]);
}
//toString
HashMap.prototype.toString=function()
{
varstr="";
for(varstrKeyinthis)
{
if(strKey.substring(0,this.prefix.length)==this.prefix)
str+=strKey.substring(this.prefix.length)
+":"+this[strKey]+"rn";
}
returnstr;
}
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"
"http://www.w3.org/TR/html4...
{HEAD}
{WELCOME}
{NUMBER} |
{FOOT}
vartmplt=newTemplate();
varroot=location.href;
root=unescape(root.substring(8,root.lastIndexOf("/")+1));
tmplt.set_root(root);
tmplt.set_file("fh","tpl/main.htm");
tmplt.set_file("head","tpl/head.htm");
tmplt.set_file("foot","tpl/foot.htm");
tmplt.set_block("fh","BROWS","rows");
tmplt.set_block("BROWS","BCOLS","cols");
tmplt.set_var("WELCOME","歡迎光臨");
for(vari=0;i<10;i++)
{
tmplt.set_var("cols","");
for(varj=0;j<10;j++)
{
tmplt.set_var("NUMBER",i+"."+j);
tmplt.parse("cols","BCOLS",true);
}
tmplt.parse("rows","BROWS",true);
}
tmplt.parse("HEAD","head",false);
tmplt.parse("FOOT","foot",false);
tmplt.pparse("out","fh",false);
【Javascript】相關(guān)文章:
對(duì)javascript的理解08-08
常用的JavaScript模式09-22
Javascript的this用法簡(jiǎn)述08-15
JavaScript 基礎(chǔ)教學(xué)09-29
JavaScript的課堂講解09-03
JavaScript常用方法匯總10-25
JavaScript中的with關(guān)鍵字07-24
高效編寫JavaScript代碼的技巧08-25