JavaScript 百分网手机站

Javascript

时间:2018-06-04 20:01:52 JavaScript 我要投稿

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;

  }

  /**

  *设置模板文件根目录

  *@paramroot

  */

  function_set_root(root)

  {

  if(!this.is_dir(root))

  {

  this.halt("set_root:"+root+"isnotadirectory.");

  }

  this.root=root;

  }

  /**

  *设定对未知模板变量的处理办法

  *@paramunknowns

  */

  function_set_unknowns(unknowns)

  {

  this.unknowns=unknowns;

  }

  /**

  *设定模板文件

  *@paramhandle

  *@paramfilename

  */

  function_set_file(handle,filename)

  {

  this.file.put(handle,this.filename(filename));

  }

  /**

  *设定模板变量

  *@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);

  }

  /**

  *设定块变量

  *@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);

  }

  /**

  *进行变量替换

  *@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;

  }

  /**

  *进行变量复制

  *@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构造函数

  */

  functionHashMap()

  {

  this.length=0;

  this.prefix="hashmap_prefix_20050524_";

  }

  /**

  *向HashMap中添加键值对

  */

  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的'集合,以数组形式返回

  */

  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的集合,以数组形式返回

  */

  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值数量

  */

  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是否存在某个key

  */

  HashMap.prototype.containsKey=function(key)

  {

  for(varstrKeyinthis)

  {

  if(strKey==this.prefix+key)

  returntrue;

  }

  returnfalse;

  }

  /**

  *判断HashMap是否存在某个value

  */

  HashMap.prototype.containsValue=function(value)

  {

  for(varstrKeyinthis)

  {

  if(this[strKey]==value)

  returntrue;

  }

  returnfalse;

  }

  /**

  *把一个HashMap的值加入到另一个HashMap中,参数必须是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模板】相关文章:

1.调用javascript

2.堆的javascript实现方法

3.JavaScript初级教程

4.javascript中的DOM方法

5.对javascript的理解

6.JavaScript中“+”的解析

7.QT理解JavaScript原型

8.javascript的方法