JavaScript Template Technology _javascript skills

Source: Internet
Author: User
/***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;
}

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

/**
* Set the method of dealing with unknown template variable
* @param unknowns
*/
function _set_unknowns (unknowns)
{
this.unknowns=unknowns;
}

/**
* Set Template file
* @param handle
* @param filename
*/
function _set_file (handle,filename)
{
This.file.put (handle,this.filename (filename));
}

/**
* Set template variable
* @param varname
* @param value
*/
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);
}

/**
* Set block variable
* @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.$1);
Str=str.replace (Re, "{" +name+ "}");
This.set_var (PARENT,STR);
}

/**
* For variable substitution
* @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=0;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;
}

/**
* Make variable copy
* @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);
}
}

/**
* Returns the replaced file
* @param varname
* @return
*/
function _p (varname)
{
Return This.finish (This.get_var (varname));
}

/**
* Merging of Parse () and P ()
* @param target
* @param handle
* @param append
* @return
*/
function _pparse (target,handle,append)
{
This.parse (Target,handle,append);
Document.writeln (THIS.P (target));
}

/**
* Load Template file
* @param handle
* @return
*/
function _loadfile (handle)
{
if (This.varkeys.containsKey (handle) && This.varvals.get (handle)!=null)
{
return true;
}
if (!this.file.containskey (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;
}

/**
* Get variables
* @param varname
* @return
*/
function _get_var (varname)
{
if (This.varvals.containsKey (varname))
Return This.varvals.get (varname);
Else
Return "";
}

/**
* List of Judgments
* @param path
* @return
*/
function _is_dir (path)
{
if (this.fso.FolderExists (path))
return true;
Else
return false;
}

/**
* Judgment Document
* @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 the processing of the string
* @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, "<!--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 (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 a collection of all keys from the HashMap, return 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;
}
/**
* Gets the collection of value from the HashMap and returns it as 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;
}
/**
* Get the number of value values for HashMap
*/
HashMap.prototype.size = function ()
{
return this.length;
}
/**
* Delete the specified value
*/
HashMap.prototype.remove = function (key)
{
Delete This[this.prefix + key];
This.length--;
}
/**
* Empty HashMap
*/
HashMap.prototype.clear = function ()
{
for (Var strkey in this)
{
if (strkey.substring (0,this.prefix.length) = = This.prefix)
Delete This[strkey];
}
this.length = 0;
}
/**
* Determine if the HashMap is empty
*/
HashMap.prototype.isEmpty = function ()
{
return this.length = = 0;
}
/**
* Determine if a key exists in HashMap
*/
HashMap.prototype.containsKey = function (key)
{
for (Var strkey in this)
{
if (strkey = = This.prefix + key)
return true;
}
return false;
}
/**
* Determine if there is a value in HashMap
*/
HashMap.prototype.containsValue = function (value)
{
for (Var strkey in this)
{
if (this[strkey] = = value)
return true;
}
return false;
}
/**
* Add a HashMap value to another hashmap, the argument must be 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] + "\ r \ n";
}
return str;
}



<!--main.htm-->

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3.org/TR/html4 ...
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Untitled Document </title>

<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>



<!--head.htm-->

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


<!--foot.htm-->

<table width= "100%" border= "1" cellspacing= "1" cellpadding= "3" >
<tr>
<td> All rights reserved: Website Dream Factory </td>
</tr>
</table>



<!--index.htm-->

<script src= "/script/template.class.js" ></script>
<script>
var tmplt=new Template ();
var root=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", "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);
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);
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.