Javascript template _ javascript skills

Source: Internet
Author: User
Javascript Template technology/*** 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 the 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 handling method for unknown template Variables
* @ Param unknowns
*/
Function _ set_unknowns (unknowns)
{
This. unknowns = unknowns;
}

/**
* Set the Template File
* @ Param handle
* @ Param filename
*/
Function _ set_file (handle, filename)
{
This. file. put (handle, this. filename (filename ));
}

/**
* Set template Variables
* @ 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 Variables
* @ 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 \ S.] *) ");
// Matcher m = p. matcher (str );
// Var rs = m. find ();
// Var t = m. group (m. groupCount ());
// This. set_var (handle, t );
Var arr=re.exe c (str );
If (arr! = Null)
This. set_var (handle, RegExp. $1 );
Str = str. replace (re, "{" + name + "}");
This. set_var (parent, str );
}

/**
* Replace variables
* @ 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 {
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;
}

/**
* Copying Variables
* @ 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 ));
}

/**
* Merge 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 the 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;
}

/**
* Getting Variables
* @ Param varname
* @ Return
*/
Function _ get_var (varname)
{
If (this. varvals. containsKey (varname ))
Return this. varvals. get (varname );
Else
Return "";
}

/**
* Directory Determination
* @ Param path
* @ Return
*/
Function _ is_dir (path)
{
If (this. fso. FolderExists (path ))
Return true;
Else
Return false;
}

/**
* File judgment
* @ 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 a key-value pair to HashMap
*/
HashMap. prototype. put = function (key, value)
{
This [this. prefix + key] = value;
This. length ++;
}
/**
* Obtain value from HashMap
*/
HashMap. prototype. get = function (key)
{
Return typeof this [this. prefix + key] = "undefined"
? Null: this [this. prefix + key];
}
/**
* Obtain the set of all keys from HashMap and return them in array form.
*/
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;
}
/**
* Obtain the value set from HashMap and return it in array form
*/
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;
}
/**
* Obtain the number of HashMap value values
*/
HashMap. prototype. size = function ()
{
Return this. length;
}
/**
* Delete a specified value
*/
HashMap. prototype. remove = function (key)
{
Delete this [this. prefix + key];
This. length --;
}
/**
* Clear 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;
}
/**
* Judge whether HashMap is empty
*/
HashMap. prototype. isEmpty = function ()
{
Return this. length = 0;
}
/**
* Determine whether a key exists in a HashMap.
*/
HashMap. prototype. containsKey = function (key)
{
For (var strKey in this)
{
If (strKey = this. prefix + key)
Return true;
}
Return false;
}
/**
* Determine whether a HashMap has a value.
*/
HashMap. prototype. containsValue = 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.
*/
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;
}





Http://www.w3.org/TR/html4...



Untitled document



{HEAD}


{WELCOME}










{NUMBER}

{FOOT}













Homepage









Copyright: www.dreamworks






Related Article

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.