Home > Web Front-end > JS Tutorial > Javascript template technology_javascript skills

Javascript template technology_javascript skills

WBOY
Release: 2016-05-16 19:14:26
Original
1030 people have browsed it

/***Template.class.js***/

함수 템플릿()
{
this.classname="Template";
this.debug=false;
this.file=new HashMap();
this.root="";
this.varkeys=new  HashMap();
this.varvals=new  HashMap();
this.unknowns="제거";
this.halt_on_error="예";
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
*/
함수 _set_root(root)
{
if(!this.is_dir(root))
{
this.halt("set_root:" root "는 디렉토리가 아닙니다.");
}
this.root=root;
}

/**
* 알 수 없는 템플릿 변수 처리 방법 설정
* @param Unknowns
*/
함수 _set_unknowns(unknowns)
{
this.unknowns=unknowns;
}

/**
* 템플릿 파일 설정
* @param 핸들
* @param 파일 이름
*/
함수 _set_file(handle,filename)
{
this.file.put(handle,this.filename(filename)) ;
}

/**
* 템플릿 변수 설정
* @param varname
* @param value
*/
함수 _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 "=================="값);
}

/**
* 블록 변수 설정
* @param parent
* @param 핸들
* @param 이름
*/
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(부모);
var re=new RegExp("([\s\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.$1);
str=str.replace(re,"{" 이름 "}");
this.set_var(parent,str);
}

/**
* 변수 대체 수행
* @param 핸들
* @return
*/
함수 _subst(handle)
{
if(!this.loadfile(handle))
{
this.halt("subst:로드할 수 없음" 핸들);
}
var str=this.get_var(handle);
var keys=this.varkeys.keySet();
//alert(keys.length);
for(var i=0;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);
반환 str;
}

/**
* 변수 복사
* @param target
* @param 핸들
* @param 추가
*/
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 handler
* @param Append
* @return
*/
function _pparse(target,handle,append)
{
this.parse(target,handle,append);
document.writeln(this.p(대상));
}

/**
* 템플릿 파일 로드
* @param 핸들
* @return
*/
function _loadfile(handle)
{
if(this.varkeys.containsKey(handle) && this.varvals.get( 핸들)!=null)
{
 return true;
}
if(!this.file.containsKey(handle))
{
 _halt("loadfile:" 핸들 "은 유효한 핸들이 아닙니다.");
 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;
}

/**
* Get variable
* @param varname
* @return
*/
function _get_var(varname)
{
if(this.varvals.containsKey(varname))
 return this.varvals.get(varname);
else
 return "";
}

/**
* Determine directory
* @param path
* @return
*/
function _is_dir(path)
{
if(this.fso.FolderExists(path))
 return true;
else
 return false;
}

/**
* Judgment file
* @param filename
* @return
*/
function _file_exists(filename)
{
if(this.fso.FileExists(filename))
 return true;
else
 return false;
}

/**
* File name processing
* @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;
}

/**
* Variable name processing
* @param varname
* @return
*/
function _varname(varname)
{
return "{" varname "}";
}

/**
* Complete string processing
* @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=="comment")
{
 str=str.replace(re,"");
}
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(0);
}
}

function _haltmsg(msg)
{
alert("Template Error:" msg);
}


/**
* HashMap constructor
*/
function HashMap()
{
   this.length = 0;
   this.prefix = "hashmap_prefix_20050524_";
}
/**
* Add key-value pairs to HashMap
*/
HashMap.prototype.put = function (key, value)
{
   this[this.prefix   key] = value;
   this.length  ;
}
/**
* Get value from HashMap
*/
HashMap.prototype.get = function(key)
{
   return typeof this[this.prefix   key] == "undefined" 
           ? null : this[this.prefix   key];
}
/**
* Get the set of all keys from HashMap and return it as an array
*/
HashMap.prototype.keySet = function()
{
   var arrKeySet = new Array();
   var index = 0;
   for(var strKey in this)
   {
       if(strKey.substring(0,this.prefix.length) == this.prefix)
           arrKeySet[index  ] = strKey.substring(this.prefix.length);
   }
   return arrKeySet.length == 0 ? null : arrKeySet;
}
/**
* Get the collection of values ​​from HashMap and return it in the form of an array
*/
HashMap.prototype.values = function()
{
   var arrValues = new Array();
   var index = 0;
   for(var strKey in this)
   {
       if(strKey.substring(0,this.prefix.length) == this.prefix)
           arrValues[index  ] = this[strKey];
   }
   return arrValues.length == 0 ? null : arrValues;
}
/**
* HashMap의 값 값 개수를 가져옵니다
*/
HashMap.prototype.size = function()
{
   return this.length;
}
/**
* 지정된 값 삭제
*/
HashMap.prototype.remove = function(key)
{
   delete this[this.prefix   key];
   this.length --;
}
/**
* 해시맵 지우기
*/
HashMap.prototype.clear = function()
{
   for(var strKey in this)
   {
       if( strKey.substring(0,this.prefix.length) == this.prefix)
           delete this[strKey];   
   }
   this.length = 0;
}
/**
* HashMap이 비어 있는지 확인
*/
HashMap.prototype.isEmpty = function()
{
   return this.length == 0;
}
/**
* HashMap에 특정 키가 존재하는지 확인
*/
HashMap.prototype.containsKey = function(key)
{
   for(var strKey in this)
   {
      if (strKey == this.prefix   key)
         return true;  
   }
   false를 반환합니다.
}
/**
* HashMap에 특정 값이 존재하는지 확인
*/
HashMap.prototype.containsValue = function(값)
{
   for(var strKey in this)
   {
      if (this[strKey] == 값)
         true를 반환합니다.  
   }
   false를 반환합니다.
}
/**
* HashMap의 값을 다른 HashMap에 추가합니다. 매개변수는 HashMap이어야 합니다.
*/
HashMap.prototype.putAll = function(map)
{
   if(map == null)
       return;
   if(map.constructor != JHashMap)
       return;
   var arrKey = map.keySet();
   var arrValue = map.values();
   for(var i in arrKey)
      this.put(arrKey[i],arrValue[i]);
}
//toString
HashMap.prototype.toString = function()
{
   var str = "";
   for(var strKey in this)

   {
       if(strKey.substring(0,this.prefix.length) == this.prefix)
           str  = strKey.substring(this .prefix.length) 
                  " : "   this[strKey]   "rn";
   }
   return str;
}





HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http ://www.w3.org/TR/html4...



无标题文档



{HEAD}


{환영합니다}



 
 
  
   
 
 
 
{NUMBER}
 
 

  

    {NUMBER}
   
 

{FOOT}




 
   
 
网站首页






 
   
 
版权所有:网站梦工厂
  <script></script>   网站首页 <script> <BR>var tmplt=new Template(); <BR>var root=location.href; <BR>root=unescape(root.substring(8,root.lastIndexOf("/")+1)); <BR>tmplt.set_root(root); <BR>tmplt.set_file("fh","tpl/main.htm"); <BR>tmplt.set_file("head","tpl/head.htm"); <BR>tmplt.set_file("foot","tpl/foot.htm"); <BR>tmplt.set_block("fh","BROWS","rows"); <BR>tmplt.set_block("BROWS","BCOLS","cols"); <BR>tmplt.set_var("WELCOME","欢迎光临"); <BR>for(var i=0;i<10;i++) <BR>{ <BR>tmplt.set_var("cols",""); <BR>for(var j=0;j<10;j++) <BR>{ <BR> tmplt.set_var("NUMBER",i+"."+j); <BR> tmplt.parse("cols","BCOLS",true); <BR>} <BR>tmplt.parse("rows","BROWS",true); <BR>} <BR>tmplt.parse("HEAD","head",false); <BR>tmplt.parse("FOOT","foot",false); <BR>tmplt.pparse("out","fh",false); <BR></script>         
版权所有:网站梦工厂








<script></script> <script> <BR>var tmplt=new Template(); <BR>var root=location.href; <BR>root=unescape(root.substring(8,root.lastIndexOf("/")+1)); <BR>tmplt.set_root(root); <BR>tmplt.set_file("fh","tpl/main.htm"); <BR>tmplt.set_file("head","tpl/head.htm"); <BR>tmplt.set_file("foot","tpl/foot.htm"); <BR>tmplt.set_block("fh","BROWS","rows"); <BR>tmplt.set_block("BROWS","BCOLS","cols"); <BR>tmplt.set_var("WELCOME","欢迎光临"); <BR>for(var i=0;i<10;i++) <BR>{ <BR>tmplt.set_var("cols",""); <BR>for(var j=0;j<10;j++) <BR>{ <BR> tmplt.set_var("NUMBER",i+"."+j); <BR> tmplt.parse("cols","BCOLS",true); <BR>} <BR>tmplt.parse("rows","BROWS",true); <BR>} <BR>tmplt.parse("HEAD","head",false); <BR>tmplt.parse("FOOT","foot",false); <BR>tmplt.pparse("out","fh",false); <BR></script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template