SoFunction
Updated on 2025-04-10

Javascript template technology

/******/

function Template()
{
="Template";
=false;
=new HashMap();
="";
=new  HashMap();
=new  HashMap();
="remove";
this.halt_on_error="yes";
this.last_error=""; 
=new ActiveXObject("");
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;
=_subst;
=_parse;
=_p;
=_pparse;
=_finish;
=_loadfile;
this.is_dir=_is_dir;
this.file_exists=_file_exists;
=_filename;
=_varname;
=_halt;
=_haltmsg;
}

/**
* Set the template file root directory
* @param root
*/
function _set_root(root)
{
if(!this.is_dir(root))
{
 ("set_root:"+root+" is not a directory.");
}
=root;
}

/**
* Set the method to deal with unknown template variables
* @param unknowns
*/
function _set_unknowns(unknowns)
{
=unknowns;
}

/**
* Set template file
* @param handle
* @param filename
*/
function _set_file(handle,filename)
{
(handle,(filename));
}

/**
* Set template variables
* @param varname
* @param value
*/
function _set_var(varname,value)
{
if(!(varname))
{
 (varname,(varname));
}
if(!(varname))
{
 (varname,value);
}
else
{
 (varname);
 (varname,value);
}
//alert(varname+"=================="+value);
}

/**
* Set block variables
* @param parent
* @param handle
* @param name
*/
function _set_block(parent,handle,name)
{
if(!(parent))

 ("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=(str);
//var rs=();
//var t=(());
//this.set_var(handle,t);
var arr=(str);
if(arr!=null)
 this.set_var(handle,RegExp.$1);
str=(re,"{"+name+"}");
this.set_var(parent,str);
}

/**
* Perform variable replacement
* @param handle
* @return 
*/
function _subst(handle)
{
if(!(handle))
{
 ("subst:unable to load "+handle);
}
var str=this.get_var(handle);
var keys=();
//alert();
for(var i=0;i<;i++)
{
 var key=keys[i];
 var re=new RegExp((key),"g")
 str=(re,(key));
}
   //alert(handle+"++++++++++++++++++"+str);
return str;
}

/**
* Perform variable copying
* @param target
* @param handle
* @param append
*/
function _parse(target,handle,append)
{
var str=(handle);
if(append)
{
 this.set_var(target,this.get_var(target)+str);
}
else
{
 this.set_var(target,str);
}
}

/**
* Return to the replaced file
* @param varname
* @return 
*/
function _p(varname)
{
return (this.get_var(varname));
}

/**
* The merger of parse() and p()
* @param target
* @param handle
* @param append
* @return 
*/
function _pparse(target,handle,append)
{
(target,handle,append);
((target));
}

/**
* Loading template file
* @param handle
* @return 
*/
function _loadfile(handle)
{
if((handle) && (handle)!=null)
{
 return true;
}
if(!(handle))
{
 _halt("loadfile:"+handle+" is not a valid handle.");
 return false;
}
var  filename=(handle);
if(!this.file_exists(filename))
{
 ("loadfile:while loading "+handle+","+filename+" does not exist.");
 return false;
}
try
{
 var fr=(filename); 
 var s=();
 if(s=="")
 {
  halt("loadfile:while loading "+handle+","+filename+" is empty.");
  return false;
 }
 this.set_var(handle,s);
}
catch(e)
{

}
return true;
}

/**
* Get variables
* @param varname
* @return 
*/
function _get_var(varname)
{
if((varname))
 return (varname);
else
 return "";
}

/**
*Judgement Directory
* @param path
* @return 
*/
function _is_dir(path)
{
if((path))
 return true;
else
 return false;
}

/**
*Judge file
* @param filename
* @return 
*/
function _file_exists(filename)
{
if((filename))
 return true;
else
 return false;
}

/**
* File name processing
* @param filename
* @return 
*/
function _filename(filename)
{
if(!this.file_exists(+filename))
{
 ("filename:file "+filename+" does not exist.");
}
return +filename;
}

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

/**
* Complete the processing of strings
* @param str
* @return 
*/
function _finish(str)
{
var re=new RegExp("{[^ \\t\\r\\n\\}]+\\}","g");
if(=="remove")
{
 str=(re,"");
}
else if(=="comment")
{
 str=(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.");
 //(0);
}
}

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


/**
* HashMap constructor
*/
function HashMap()
{
    = 0;
    = "hashmap_prefix_20050524_";
}
/**
* Add key-value pairs to HashMap
*/
 = function (key, value)
{
   this[ + key] = value;
    ++;
}
/**
* Get value from HashMap
*/
 = function(key)
{
   return typeof this[ + key] == "undefined" 
           ? null : this[ + key];
}
/**
* Get a collection of all keys from a HashMap and return it in an array
*/
 = function()
{
   var arrKeySet = new Array();
   var index = 0;
   for(var strKey in this)
   {
       if((0,) == )
           arrKeySet[index ++] = ();
   }
   return  == 0 ? null : arrKeySet;
}
/**
* Get the collection of value from HashMap and return it in an array
*/
 = function()
{
   var arrValues = new Array();
   var index = 0;
   for(var strKey in this)
   {
       if((0,) == )
           arrValues[index ++] = this[strKey];
   }
   return  == 0 ? null : arrValues;
}
/**
* Get the number of value values ​​of HashMap
*/
 = function()
{
   return ;
}
/**
* Delete the specified value
*/
 = function(key)
{
   delete this[ + key];
    --;
}
/**
* Clear HashMap
*/
 = function()
{
   for(var strKey in this)
   {
       if((0,) == )
           delete this[strKey];   
   }
    = 0;
}
/**
* Judge whether HashMap is empty
*/
 = function()
{
   return  == 0;
}
/**
* Judge whether there is a key in HashMap
*/
 = function(key)
{
   for(var strKey in this)
   {
      if(strKey ==  + key)
         return true;  
   }
   return false;
}
/**
* Judge whether there is a value in HashMap
*/
 = function(value)
{
   for(var strKey in this)
   {
      if(this[strKey] == value)
         return true;  
   }
   return false;
}
/**
* Add the value of a HashMap to another HashMap, the parameter must be HashMap
*/
 = function(map)
{
   if(map == null)
       return;
   if( != JHashMap)
       return;
   var arrKey = ();
   var arrValue = ();
   for(var i in arrKey)
      (arrKey[i],arrValue[i]);
}
//toString
 = function()
{
   var str = "";
   for(var strKey in this)

   {
       if((0,) == )
             str += () 
                 + " : " + this[strKey] + "\r\n";
   }
   return str;
}



<!--          -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http:///TR/html4...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Unt titled document</title>
</head>

<body>
<p>{HEAD}</p>
<p>{WELCOME}
</p>
<table width="100%"  border="1" cellspacing="1" cellpadding="3">
 <!-- BEGIN BROWS -->
 <tr>
  <!-- BEGIN BCOLS -->
   <td>{NUMBER}</td>
<!-- END BCOLS --> 
 </tr>
 <!-- END BROWS -->
</table>
<p>{FOOT}</p>
</body>
</html>



<!--             -->

<table width="100%"  border="1" cellspacing="1" cellpadding="3">
 <tr>
<td>Homepage</td>
 </tr>
</table>


<!--            -->

<table width="100%"  border="1" cellspacing="1" cellpadding="3">
 <tr>
<td>Copyright: Website DreamWorks</td>
 </tr>
</table>



<!--         -->

<script src="/script/"></script>
<script>
var tmplt=new Template();
var root=;
root=unescape((8,("/")+1));
tmplt.set_root(root);
tmplt.set_file("fh","tpl/");
tmplt.set_file("head","tpl/");
tmplt.set_file("foot","tpl/");
tmplt.set_block("fh","BROWS","rows");
tmplt.set_block("BROWS","BCOLS","cols");
tmplt.set_var("WELCOME","Welcome");
for(var i=0;i<10;i++)
{
tmplt.set_var("cols","");
for(var j=0;j<10;j++)
{
 tmplt.set_var("NUMBER",i+"."+j);
 ("cols","BCOLS",true);
}
("rows","BROWS",true);
}
("HEAD","head",false);
("FOOT","foot",false);
("out","fh",false);
</script>