通知模板范文 Javascript模板技术
Javascript模板技术
/***Template class js***/
function Template(){ this classname= Template ; this debug=false; this file=new HashMap(); this root= ; this varkeys=new HashMap(); this varvals=new HashMap(); this unknowns= remove ; this halt_on_error= yes ; this last_error= ; this fso=new ActiveXObject( 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;}
/** * 设置模板文件根目录 * @param root */function _set_root(root){ if(!this is_dir(root)) { this halt( set_root: +root+ is not a directory ); } this root=root;} /** * 设定对未知模板变量的处理办法 * @param unknowns */function _set_unknowns(unknowns){ this unknowns=unknowns;} /** * 设定模板文件 * @param handle * @param filename */function _set_file(handle filename){ this file put(handle this filename(filename));} /** * 设定模板变量 * @param varname * @param value */function _set_var(varname value){ if(!ntainsKey(varname)) { this varkeys put(varname this varname(varname)); } if(!ntainsKey(varname)) { this varvals put(varname value); } else { this varvals remove(varname); this varvals put(varname value); } //alert(varname+ ================== +value);}
/** * 设定块变量 * @param parent * @param handle * @param name */function _set_block(parent handle name){ if(!this loadfile(parent)) { this halt( subst:unable to load +parent); } if(name== ) { name=handle; } var str=this get_var(parent); var re=new RegExp( <! \s+BEGIN + handle + \s+ >([\s\S ]*)<! \s+END + handle + \s+ > ); //Matcher m=p matcher(str); //var rs=m find(); //var t=m group(m groupCount()); //this set_var(handle t); var arr=re exec(str); if(arr!=null) this set_var(handle RegExp $ ); str=str replace(re { +name+ } ); this set_var(parent str);}
/** * 进行变量替换 * @param handle * @return */function _subst(handle){ if(!this loadfile(handle)) { this halt( subst:unable to load +handle); } var str=this get_var(handle); var keys=this varkeys keySet(); //alert(keys length); for(var i= ;i<keys length;i++) { var key=keys[i]; var re=new RegExp(this varkeys get(key) g ) str=str replace(re this varvals get(key)); } //alert(handle+ ++++++++++++++++++ +str); return str;} /** * 进行变量复制 * @param target * @param handle * @param append */function _parse(target handle append){ var str=this subst(handle); if(append) { this set_var(target this get_var(target)+str); } else { this set_var(target str); }}
/** * 返回替换后的文件 * @param varname * @return */function _p(varname){ return this finish(this get_var(varname));} /** * parse()和p()的合并 * @param target * @param handle * @param append * @return */function _pparse(target handle append){ this parse(target handle append); document writeln(this p(target));}
/** * 加载模板文件 * @param handle * @return */function _loadfile(handle){ if(ntainsKey(handle) && this varvals get(handle)!=null) { return true; } if(!ntainsKey(handle)) { _halt( loadfile: +handle+ is not a valid handle ); return false; } var filename=this file get(handle); if(!this file_exists(filename)) { this halt( loadfile:while loading +handle+ +filename+ does not exist ); return false; } try { var fr=this fso OpenTextFile(filename); var s=fr ReadAll(); if(s== ) { halt( loadfile:while loading +handle+ +filename+ is empty ); return false; } this set_var(handle s); } catch(e) { } return true;}
/** * 获取变量 * @param varname * @return */function _get_var(varname){ if(ntainsKey(varname)) return this varvals get(varname); else return ;}

/** * 判断目录 * @param path * @return */function _is_dir(path){ if(this fso FolderExists(path)) return true; else return false;} /*** 判断文件* @param filename* @return */function _file_exists(filename){ if(this fso FileExists(filename)) return true; else return false;} /** * 文件名处理 * @param filename * @return */function _filename(filename){ if(!this file_exists(this root+filename)) { this halt( filename:file +filename+ does not exist ); } return this root+filename;} /** * 变量名处理 * @param varname * @return */function _varname(varname){ return { +varname+ } ;} /** * 完成字符串的处理 * @param str * @return */function _finish(str){ var re=new RegExp( {[^ \t\r\n\}]+\} g ); if(this unknowns== remove ) { str=str replace(re ); } else if(this unknowns== ment ) { str=str replace(re <! Template Variable undefined > ); } else { ; } return str;}
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( ); }} function _haltmsg(msg){ alert( Template Error: +msg);}
/** * HashMap构造函数 */function HashMap(){ this length = ; this prefix = hashmap_prefix_ _ ;}/** * 向HashMap中添加键值对 */HashMap prototype put = function (key value){ this[this prefix + key] = value; this length ++;}/** * 从HashMap中获取value值 */HashMap prototype get = function(key){ return typeof this[this prefix + key] == undefined ? null : this[this prefix + key];}/** * 从HashMap中获取所有key的集合 以数组形式返回 */HashMap prototype keySet = function(){ var arrKeySet = new Array(); var index = ; for(var strKey in this) { if(strKey substring( this prefix length) == this prefix) arrKeySet[index ++] = strKey substring(this prefix length); } return arrKeySet length == ? null : arrKeySet;}/** * 从HashMap中获取value的集合 以数组形式返回 */HashMap prototype values = function(){ var arrValues = new Array(); var index = ; for(var strKey in this) { if(strKey substring( this prefix length) == this prefix) arrValues[index ++] = this[strKey]; } return arrValues length == ? null : arrValues;}/** * 获取HashMap的value值数量 */HashMap prototype size = function(){ return this length;}/** * 删除指定的值 */HashMap prototype remove = function(key){ delete this[this prefix + key]; this length ;}/** * 清空HashMap */HashMap prototype clear = function(){ for(var strKey in this) { if(strKey substring( this prefix length) == this prefix) delete this[strKey]; } this length = ;}/** * 判断HashMap是否为空 */HashMap prototype isEmpty = function(){ return this length == ;}/** * 判断HashMap是否存在某个key */HntainsKey = function(key){ for(var strKey in this) { if(strKey == this prefix + key) return true; } return false;}/** * 判断HashMap是否存在某个value */HntainsValue = function(value){ for(var strKey in this) { if(this[strKey] == value) return true; } return false;}/** * 把一个HashMap的值加入到另一个HashMap中 参数必须是HashMap */HashMap prototype putAll = function(map){ if(map == null) return; if(nstructor != JHashMap) return; var arrKey = map keySet(); var arrValue = map values(); for(var i in arrKey) this put(arrKey[i] arrValue[i]);}//toStringHashMap prototype toString = function(){ var str = ; for(var strKey in this)
{ if(strKey substring( this prefix length) == this prefix) str += strKey substring(this prefix length) + : + this[strKey] + rn ; } return str;}
<! >
<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN ><><head><meta equiv= Content Type content= text/; charset=gb ><title>无标题文档</title></head>
<body><p>{HEAD}</p><p>{WELE}</p><table width= % border= cellspacing= cellpadding= > <! BEGIN BROWS > <tr> <! BEGIN BCOLS > <td>{NUMBER}</td> <! END BCOLS > </tr> <! END BROWS ></table><p>{FOOT}</p></body></>
<! >
<table width= % border= cellspacing= cellpadding= > <tr> <td>网站首页</td> </tr></table>
<! >
<table width= % border= cellspacing= cellpadding= > <tr> <td>版权所有 网站梦工厂</td> </tr></table>
<! >
lishixinzhi/Article/program/Java/Javascript/201311/25486