<% '=================================== =============================================File: template.class.asp ASP page template "Author : Woods' Date: 2005-04-01 'Website: http://www.54youngor.com/' email: letsflytogether.com 'Reference: phplib, kkttemplate' ============== ============================================================================================================================================================================================================= ======= class Template private m_classname '/ * if set, echo assignments * / private m_debug' / * $ file [handle] = "filename"; * / private m_file '/ * relative filenames are relative to this pathname * / Private m_root '/ * $ VARKEYS [key] = "key"; $ varvals [key] = "value"; * / private m_varkeys private m_varvals' / * "remove" => remove undefined variables' * "comment" = > replace undefined variables with comments' * "keep" => keep undefined variables' * / private m_unknowns' / * "yes" => halt, "report" => report error, continue, "no" => ignore error quietly * / Private m_halt_on_error '
/ * Last error message is retained here * / private m_last_error private m_regexp private m_fso private sub class_initialize m_classname = "Template" m_debug = true set m_file = Server.CreateObject ( "Scripting.Dictionary") m_root = Server.MapPath ( ".") & "/" set m_varkeys = Server.CreateObject ( "Scripting.Dictionary") set m_varvals = Server.CreateObject ( "Scripting.Dictionary") m_unknowns = "remove" m_halt_on_error = "yes" m_last_error = "" set m_regexp = new RegExp m_regexp .IgnoreCase = True m_regexp.Global = True set m_fso = Server.CreateObject ( "Scripting.FileSystemObject") end sub private sub class_terminate set m_file = nothing set m_varkeys = nothing set m_varvals = nothing set m_regexp = nothing set m_fso = nothing end sub ' / * public: set_root (pathname root) '* root: new template Directory.' * / public sub set_root (Byval root) IS_DIR (M_Root & Root & "/") THEN HALT ("SET_ROOT: Root Is Not a directory.") End if m_root = m_root & root & "end sub '/ * public: set_unknowns (Enum $ unknowns) '* Unknowns: "remove", "comment", "keep"' * '* / public sub set_unknowns (ByVal unknowns) m_unknowns = unknowns end sub' / * public: set_file (array $ filelist) '* filelist: array of handle Filename pairs. '*' * public: set_file (string $ handle, string $ filename) '* handle: handle for a filename,' * filename: name of template File '
* / Public sub set_file (byval Handle, Byval File) IF not m_file.exists (file) Then m_file.add handle, filename (file) end if end sub '/ * public: set_var (array $ values)' * VALUES: Array of variable name, value pairs' * '* public:. set_var (string $ varname, string $ value)' * varname: name of a variable that is to be defined '* value: value of that variable' * / public sub set_var (Byval name, byval value) if not m_VARKEYS.EXISTS (Name) Then m_varkeys.add name, varname (name) Endiff name, value else m_varvals.item (name) = Value end if 'response.write (Name & "====" & Value & "
--------------------------- --------------------------
")") End Sub '/ * public: set_block (String $ PARENT, STRING $ HANDLE, STRING $ Name = "") '* extract the template $ handle from $ parent,' * place variable {$ name} instead. '* / public sub set_block (byval parent, byval handle, byval name) if not loadfile (parent) THEN HALT ("SUBS T: unable to load. "& pent) end if if name =" "" "" " ([/ S / S.] *) "set matches = m_regexp.execute (str) str = m_regexp.replace (Str," {" & name & "}") for Each Match in matches set_var handle, match.submatches (0) Next SET_VAR PARENT, STR End Sub '/ * public: get_var (string varName)' * varName: name of variable. '*'
* Public: get_var (array varname) '* varname: array of variable names' * / public function get_var (ByVal name) get_var = m_varvals.Item (name) end function '/ * public: subst (string $ handle)' * handle : handle of template where variables are to be substituted '* / public function subst (ByVal handle) if not loadfile (handle) then halt. ( "subst: unable to load" & handle) end if str = get_var (handle) keys = M_VARKEYS.KEYS for i_i = 0 to m_varkeys.count-1 m_regexp.pattern = m_varkeys.item (keys (i_i)) str = m_regExp.replace (Str, M_VARVALS.Item (Keys (i_i))) Next Subst = Str End Function '/ * public: parse (String $ Target, String $ Handle, Boolean Append)' * Public: Parse (String $ TARGET, Array $ Handle, Boolean Append) '* Target: Handle of Variable To Generate' * Handle: Handle of Template to Substitution '* append: append to target handle' * / public sub parse (byval target, byval handle, byval append) str = subs (handle) if append = true kil ten, get_var (targ), Get_var (targ), Get_var (TARG) SUBST (HANDE) IF APPENT = ET) & str else set_var target, str even if End sub '/ * public: finish (string $ str)' * str: string to finish. '* / public function finish (byval str) Select Case M_Unknowns Case "Keep" finish = Str Case "Remove" M_Regexp.pattern = "{[^ / T / R / N}] }" Finish = m_Regexp.Replace (STR, ") Case" Comment "m_regexp.pattern =" {[^ / T / r / n}] } "Finish = m_regExp.replace (str," ") Case Else Finish =
Str End Select End Function '/ * Public: P (String $ Varname)' * VarName: Name of Variable To Print. '* / Public Sub P (Byval Name) Response.write (finish (get_var (name)) End Sub '/ * public: p (string $ varname)' * varName: name of variable to print. '* / public sub pParse (Byval target, byval handle, byval append) PARSE TARGET, HANDLE, APPEND P TARGET End Sub' Private * *********************************************************** ********** private function is_dir (ByVal path) is_dir = m_fso.FolderExists (path) end function private function file_exists (ByVal file) file_exists = m_fso.FileExists (file) end function '/ * private: loadfile (String $ Handle) '* Handle: Load file defined by handle, if it is not loaded yet.' * / private function loadfile (byval Handle) if not (m_varkeys.exists (handle) and m_varvals.item (handle) <> "") Then IF m_file.item (Handle) = "" "" LoadFile: "& Handle &" IS Not a Valid Handle. ") end if name = m_file.Item Handle) The Halt ("LoadFile: While Loading" & Handle & "," & Name & "Does Not Exist.") end if set fh = m_fso.opentextfile (name) str = fH.Readall if Str = "" THEN HALT ("LoadFile: While" & Handle & "", "& Name &" IS Empty. ") Endiff set fH = nothing set_var handle, strued if loadingfile = true end function '/ * private: Filename: name to be completed. '* / private function filename (byval file) if not file_exists (m_root)
file) then halt ( "filename: file" & file & "does not exist.") end if filename = m_root & file end function '/ * private: varname ($ varname)' * varname: name of a replacement variable to be protected '*. / Private function varname (byval name) varname = "{" & name & "}" end function '/ * public: hald (string $ msg)' * msg: error message to show. '* / Private sub halt (byval msg) m_last_error = msg if m_halt_on_error <> "no" Then Haltmsg (msg) end if imp_halt_on_error = "yes" Then Response.write (" halted. b>") response.end () end if End sub '/ * Public, Override: Haltmsg ($ msg) '* msg: error message to show.' * / private sub haltmsg (Byval MSG) Response.write (" Template Error: B>" & MSG & " {foot} p> body> html>
") Response.end () end sub end class%> - begin bcol -> {number} td> tr> table>