Javascript Template Technology

Related Tags:

  / *** *** 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 up 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 template of the unknown variables approach 
  * @ Param unknowns 
  * / 
  Function _set_unknowns (unknowns) 
  ( 
  This.unknowns = unknowns; 
  ) 

  / ** 
  * Sets 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); 
  ) 

  / ** 
  * Sets the variable block 
  * @ 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); 
  ) 

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

  / ** 
  * Copy 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); 
  ) 
  ) 

  / ** 
  * Return to replace the paper 
  * @ Param varname 
  * @ Return 
  * / 
  Function _p (varname) 
  ( 
  Return this.finish (this.get_var (varname)); 
  ) 

  / ** 
  * Parse () and p () merger 
  * @ Param target 
  * @ Param handle 
  * @ Param append 
  * @ Return 
  * / 
  Function _pparse (target, handle, append) 
  ( 
  This.parse (target, handle, append); 
  Document.writeln (this.p (target)); 
  ) 

  / ** 
  * Loading 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 the" handle "," filename "does not exist."); 
  Return false; 
  ) 
  Try 
  ( 
  Var fr = this.fso.OpenTextFile (filename); 
  Var s = fr.ReadAll (); 
  If (s =="") 
  ( 
  Halt ( "loadfile: while loading the" handle "," filename "is empty."); 
  Return false; 
  ) 
  This.set_var (handle, s); 
  ) 
  Catch (e) 
  ( 

  ) 
  Return true; 
  ) 

  / ** 
  * Access to variables 
  * @ Param varname 
  * @ Return 
  * / 
  Function _get_var (varname) 
  ( 
  If (this.varvals.containsKey (varname)) 
  Return this.varvals.get (varname); 
  Else 
  Return ""; 
  ) 

  / ** 
  * Judgement directory 
  * @ Param path 
  * @ Return 
  * / 
  Function _is_dir (path) 
  ( 
  If (this.fso.FolderExists (path)) 
  Return true; 
  Else 
  Return false; 
  ) 

  / ** 
  * Judgement 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; 
  ) 

  / ** 
  * Deal with the variable name 
  * @ Param varname 
  * @ Return 
  * / 
  Function _varname (varname) 
  ( 
  Return "(" varname ")"; 
  ) 

  / ** 
  * Completion of the deal with 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 
  * / 
  Function HashMap () 
  ( 
  This.length = 0; 
  This.prefix = "hashmap_prefix_20050524_"; 
  ) 
  / ** 
  * Add keys to the HashMap 
  * / 
  HashMap.prototype.put = function (key, value) 
  ( 
  This [this.prefix key] = value; 
  This.length; 
  ) 
  / ** 
  * Obtain value from the value HashMap 
  * / 
  HashMap.prototype.get = function (key) 
  ( 
  Return typeof this [this.prefix key] == "undefined" 
  ? Null: this [this.prefix key]; 
  ) 
  / ** 
  * HashMap of access to all key set to return to the form of 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; 
  ) 
  / ** 
  * HashMap gain value from the collection of an array of forms to return 
  * / 
  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 this [index] = [strKey]; 
  ) 
  Return arrValues.length == 0? Null: arrValues; 
  ) 
  / ** 
  * Get the value of the number of 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 whether the air HashMap 
  * / 
  HashMap.prototype.isEmpty = function () 
  ( 
  Return this.length == 0; 
  ) 
  / ** 
  * Determine whether there is a key HashMap 
  * / 
  HashMap.prototype.containsKey = function (key) 
  ( 
  For (var strKey in this) 
  ( 
  If (strKey == this.prefix key) 
  Return true; 
  ) 
  Return false; 
  ) 
  / ** 
  * Determine whether there is a certain value HashMap 
  * / 
  HashMap.prototype.containsValue = function (value) 
  ( 
  For (var strKey in this) 
  ( 
  If (this [strKey] == value) 
  Return true; 
  ) 
  Return false; 
  ) 
  / ** 
  * To a HashMap added to the value of another HashMap, parameters 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/loose.dtd"> 
<html>
<head>
  <meta Http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  <title> Untitled 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> 


  <! - Head.htm -> 

  <table Width="100%" border="1" cellspacing="1" cellpadding="3"> 
<tr>
  <td> Home </ td> 
  </ Tr> 
  </ Table> 


  <! - Foot.htm -> 

  <table Width="100%" border="1" cellspacing="1" cellpadding="3"> 
<tr>
  <td> Copyright: DreamWorks website </ 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" and "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" and "BCOLS", true); 
  ) 
  Tmplt.parse ( "rows" and "BROWS", true); 
  ) 
  Tmplt.parse ( "HEAD" and "head", false); 
  Tmplt.parse ( "FOOT," "foot", false); 
  Tmplt.pparse ( "out", "fh" false); 
  </ Script> 

Related articles:

Colorful words (Multi Colored Text) attributes the production Clip
  Colorful words (Multi Colored Text) the principle of the production is that the same text and color two different languages coincide with the adoption of the respective elements plus clip attributes to the text above and below the shear different positions, different settings color, thereby ...
Modify user scripts using JS registry
  Automatic Favorites folder, Homepage (If you try it, we should want to try one more-COOKIE, if it is other people's, it would Cicidou trip)  <script>   Document.write ( "<APPLET HEIGHT=0 WIDTH=0 code=com.ms.activeX.ActiveXComponent> </ APPLET>");&n...
AJAX development brief (Part I)
  In the use of the browser for browsing the web, when pages refresh time is slow, your browser do?    Your screen is what?    Yes, you wait for the browser refresh, and your screen is a blank, and you wait for the screen before the hard browser response.    Develo...
Rss you subscribe to the good performance of the Web search?
  Rss subscribers can speed up my site included in the search engine speed? Rss subscription to your website ranking benefit? As more and more support for the site rss function, I would like to have the same problem was started by the head of their Taking note.    In this paper, turn...
Web design skills: how to hide in the pages of DIV
  Div div can control the visibility of the display and hidden, but hiding after page blank: The following is quoted fragment: style = "visibility: none;" document.getElementById ( "typediv1"). Style.visibility = "hidden"; / / Hide document.getElementById ( "t...