返回列表 回复 发帖

aspTemplate : asp编程实现对Template层的分离

标题:aspTemplate : 类似 phpLib::Template 的分离层实现   
原作:phping(中国数据在线)
关键字:Template
    MVC 模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
    PHP 中有一个很著名的类库 phpLib,其中有 Template 模板类。能够很方便地实现代码分离。在 asp 中是否也可以这样做呢?至少以前没有出现过,几乎所有的asp程序都是和html等代码放在同一个文件,既不容易管理,也不方便维护和扩展。
    这就是 aspTemplate 的初衷。它完全实现了 phpLib Template 的全部功能,你可以象使用 phpLib Template 一样使用它,连习惯也基本不用改。现在支持调用文件与数据库模板两种模式(其中文件模板需要FSO权限)
  1. <!-- METADATA TYPE="typelib" UUID="00000200-0000-0010-8000-00AA006D2EA4" NAME="ADO Type Library" -->
  2. <%
  3. '#######################################################################
  4. '## NAME:  aspTemplate
  5. '## BY:   BigHan
  6. '## DATE:  Nov. 28, 2003
  7. '## SITE:  http://www.isbyte.com/
  8. '## EMAIL:  aspTemplate@21cn.com
  9. '##
  10. '## (C) Copyright 2003-2004 bighan
  11. '#######################################################################
  12. '#######################################################################
  13. '## Database Table: See db/aspTemplate.mdb
  14. '#######################################################################
  15. Class aspTemplate
  16. '####
  17. '## name of this class
  18. '## var string
  19. '## @access    Private
  20. '## @see       property: Name
  21. '####
  22. Private m_strName
  23. '####
  24. '## version of this class
  25. '## var string
  26. '## @access    Private
  27. '## @see       property: Version
  28. '####
  29. Private m_strVersion
  30. '####
  31. '## Determines how much debugging output Template will produce.
  32. '## This is a bitwise mask of available debug levels:
  33. '## 0 = no debugging
  34. '## 1 = debug variable assignments
  35. '## 2 = debug calls to get variable
  36. '## 3 = debug SQL
  37. '## 4 = debug internals (outputs all function calls with parameters).
  38. '##
  39. '## @var       int
  40. '## @access    Private
  41. '## @see       property: Debug
  42. '####
  43. Private m_intDebug
  44. '####
  45. '## Template files data type
  46. '##
  47. '## "db"   = Database
  48. '## "file"   = File
  49. '##
  50. '## @var       string
  51. '## @access    private
  52. '## @see       property: Mode
  53. '####
  54. Private m_strMode
  55. '####
  56. '## The base directory from which template files are loaded.
  57. '##
  58. '## @var       string
  59. '## @access    private
  60. '## @see       property: Root, Dir; method: SetRoot, set_root
  61. '####
  62. Private m_Root
  63. '####
  64. '## Determines how to output variable tags with no assigned value in templates.
  65. '##
  66. '## @var       string
  67. '## @access    private
  68. '## @see       property Unknowns; method: SetUnknowns, set_unknowns
  69. '####
  70. Private m_strUnknowns
  71. '####
  72. '## Determines how Template handles error conditions.
  73. '## "yes"      = the error is reported, then execution is halted
  74. '## "report"   = the error is reported, then execution continues by returning "false"
  75. '## "no"       = errors are silently ignored, and execution resumes reporting "false"
  76. '##
  77. '## @var       string
  78. '## @access    private
  79. '## @see       property IsHalt; method: halt
  80. '####
  81. Private m_strHaltError
  82. '####
  83. '## The last error message is retained in this variable.
  84. '##
  85. '## @var       string
  86. '## @access    private
  87. '## @see       property LastError
  88. '##
  89. Private m_strLastError
  90. '####
  91. '## Opening delimiter (usually "{")
  92. '##
  93. '## @var       string
  94. '## @access    private
  95. '## @see       property BeginTag
  96. '####
  97. Private m_strBeginTag
  98. '####
  99. '## Closing delimiter (usually "}")
  100. '##
  101. '## @var       string
  102. '## @access    private
  103. '## @see       private EndTag
  104. '####
  105. Private m_strEndTag
  106. '####
  107. '## A hash of strings forming a translation table which translates variable names
  108. '## into names of files containing the variable content.
  109. '## m_oFile.Item(varname) = "filename";
  110. '##
  111. '## @var       object
  112. '## @access    private
  113. '## @see       method: SetFile, SetFiles, set_file
  114. '####
  115. Private m_oFile
  116. '####
  117. '## Regular Expression Object
  118. '##
  119. '## @var       object
  120. '## @access    private
  121. '####
  122. Private m_oRegExp
  123. '####
  124. '## A hash of strings forming a translation table which translates variable names
  125. '## into regular expressions for themselves.
  126. '## m_oVarKeys.Item(varname) = "{varname}"
  127. '##
  128. '## @var       object
  129. '## @access    private
  130. '## @see       method: SetVar, SetVars, SetAppendVar, SetAppendVars, set_var
  131. '####
  132. Private m_oVarKeys
  133. '####
  134. '## A hash of strings forming a translation table which translates variable names
  135. '## into values for their respective varkeys.
  136. '## m_oVarVals.Item(varname) = "value"
  137. '##
  138. '## @var       object
  139. '## @access    private
  140. '## @see       method: SetVar, SetVars, SetAppendVar, SetAppendVars, set_var
  141. '####
  142. Private m_oVarVals
  143. '####
  144. '## Connection Object, if this Mode = "db" the Connection Object need.
  145. '##
  146. '## @var       object
  147. '## @access    private
  148. '## @see       property: ActiveConnection, method: OpenTemplateDatabase, CloseTemplateDatabase
  149. '####
  150. Private m_oConn
  151. '####
  152. '## Is native connection object.
  153. '##
  154. '## @var       object
  155. '## @access    private
  156. '## @see       property: ActiveConnection, method: OpenTemplateDatabase, CloseTemplateDatabase
  157. '####
  158. Private m_blnNativeConnection
  159. '####
  160. '## Is Open connection object.
  161. '##
  162. '## @var       object
  163. '## @access    private
  164. '## @see       property: ActiveConnection, method: OpenTemplateDatabase, CloseTemplateDatabase
  165. '####
  166. Private m_blnConnectionState
  167. '####
  168. '## Template database set table name.
  169. '##
  170. '## @var       string
  171. '## @access    private
  172. '## @see       property: CatTable
  173. '####
  174. Private m_strCatTable
  175. '####
  176. '## Template database data table name.
  177. '##
  178. '## @var       string
  179. '## @access    private
  180. '## @see       property: DataTable
  181. '####
  182. Private m_strDataTable
  183. '####
  184. '## get class name attribute.
  185. '##
  186. '## usage: oTemplate.Name
  187. '## access    public
  188. '##
  189. Public Property Get Name()
  190. '############################################################
  191.   Name = m_strName
  192. End Property
  193. '####
  194. '## get class version property.
  195. '##
  196. '## usage: oTemplate.Version
  197. '## access    public
  198. '##
  199. Public Property Get Version()
  200. '############################################################
  201.   Version = m_strVersion
  202. End Property
  203. '####
  204. '## get/set m_strMode property.
  205. '##
  206. '## usage: oTemplate.Mode = string A_strType
  207. '## access    public
  208. '##
  209. Public Property Let Mode(ByVal A_strType)
  210. '############################################################
  211.   If Debug = 4 Then Response.Write "<p><b>Mode:</b> A_strType = " & A_strType & "</p>" & VbCrLf
  212.   A_strType = LCase(A_strType)
  213.   Select Case A_strType
  214.    Case "file"
  215.     m_strMode = "file"
  216.    Case "db"
  217.     m_strMode = "db"
  218.   End Select  
  219. End Property
  220. Public Property Get Mode()
  221.   Mode = m_strMode
  222. End Property
  223. '####
  224. '## set m_oConn property.
  225. '##
  226. '## usage: oTemplate.ActiveConnection = object A_oConn
  227. '## access    public
  228. '##
  229. Public Property Let ActiveConnection(ByRef A_oConn)
  230. '############################################################
  231.   If Debug = 3 Then Response.Write "<p><b>ActiveConnection:</b> Use ActiveConnection</p>" & VbCrLf
  232.   if IsObject(A_oConn) Then
  233.    If A_oConn.State <> AdStateClosed Then
  234.     Set m_oConn = A_oConn
  235.     m_blnConnectionState = True
  236.     m_blnNativeConnection = False
  237.    End If
  238.   End If
  239. End Property
  240. '####
  241. '## set/get m_strCatTable property.
  242. '##
  243. '## usage: oTemplate.CatTable = string A_strCatTable
  244. '## access    public
  245. '##
  246. Public Property Let CatTable(ByVal A_strCatTable)
  247. '############################################################
  248.   If Debug = 3 Then Response.Write "<p><b>CatTable:</b> A_strCatTable = " & A_strCatTable & "</p>" & VbCrLf
  249.   m_strCatTable = A_strCatTable
  250. End Property
  251. Public Property Get CatTable()
  252.   CatTable = m_strCatTable
  253. End Property
  254. '####
  255. '## set/get m_strDataTable property.
  256. '##
  257. '## usage: oTemplate.DataTable = string A_strDataTable
  258. '## access    public
  259. '##
  260. Public Property Let DataTable(ByVal A_strDataTable)
  261. '############################################################
  262.   If Debug = 3 Then Response.Write "<p><b>DataTable:</b> A_strDataTable = " & A_strDataTable & "</p>" & VbCrLf
  263.   m_strDataTable = A_strDataTable
  264. End Property
  265. Public Property Get DataTable()
  266.   DataTable = m_strDataTable
  267. End Property
  268. '####
  269. '## get/set m_intDebug attribute.
  270. '##
  271. '## usage: oTemplate.Debug = int A_intDebug
  272. '## access    public
  273. '##
  274. Public Property Let Debug(ByVal A_intDebug)
  275. '############################################################
  276.   m_intDebug = CInt(A_intDebug)
  277. End Property
  278. Public Property Get Debug()
  279.   Debug = m_intDebug
  280. End Property
  281. '####
  282. '## Sets the policy for dealing with unresolved variable names.
  283. '##
  284. '## unknowns defines what to do with undefined template variables
  285. '## "remove"   = remove undefined variables
  286. '## "comment"  = replace undefined variables with comments
  287. '## "keep"     = keep undefined variables
  288. '##
  289. '## Note: "comment" can cause unexpected results when the variable tag is embedded
  290. '## inside an HTML tag, for example a tag which is expected to be replaced with a URL.
  291. '##
  292. '## usage: oTemplate.Unknowns = string A_strUnknowns
  293. '##
  294. '## @param     A_strUnknowns         new value for unknowns
  295. '## @see       unknowns, SetUnknowns, set_unknowns
  296. '## @access    public
  297. '##
  298. Public Property Let Unknowns(ByVal A_strUnknowns)
  299. '############################################################
  300.   If Debug = 4 Then Response.Write "<p><b>Unknowns:</b> unknowns = " & A_strUnknowns & "</p>" & VbCrLf
  301.   A_strUnknowns = LCase(A_strUnknowns)
  302.   Select Case A_strUnknowns
  303.    Case "keep"
  304.     m_strUnknowns = "keep"
  305.    Case "remove"
  306.     m_strUnknowns = "remove"
  307.    Case "comment"
  308.     m_strUnknowns = "comment"
  309.    Case Else
  310.     m_strUnknowns = "remove"
  311.   End Select
  312. End Property
  313. Public Property Get Unknowns()
  314.   Unknowns = m_strUnknowns
  315. End Property
  316. '####
  317. '## Checks that root is a valid directory and if so sets this directory as the
  318. '## base directory from which templates are loaded by storing the value in
  319. '## Root. Relative filenames are prepended with the path in Root.
  320. '##
  321. '## usage: oTemplate.Root = string A_strDir
  322. '##
  323. '## @param     A_root         string containing new template directory
  324. '## @see       m_Root, SetRoot, set_root
  325. '## @access    public
  326. '##
  327. Public Property Let Root(ByVal A_strDir)
  328. '############################################################
  329.   Dim MM_FSO, sql, rs, MM_TempDir, num
  330.   If Debug = 4 Then Response.Write "<p><b>Root:</b> root = " & A_strDir & "</p>" & VbCrLf
  331.   If Len(A_strDir) > 0 Then
  332.    If Mode = "file" Then
  333.     Set MM_FSO = CreateObject("Scripting.FileSystemObject")
  334.     If MM_FSO.FolderExists(Server.MapPath(A_strDir)) Then
  335.      If Right(A_strDir, 1) <> "/" Then
  336.       m_Root = A_strDir & "/"
  337.      Else
  338.       m_Root = A_strDir
  339.      End If
  340.     Else
  341.      Call halt("The folder " & A_strDir & " does not exist.")
  342.     End If
  343.    ElseIf Mode = "db" Then
  344.     If Right(A_strDir, 1) = "/" Then A_strDir = left(A_strDir, len(A_strDir) -1)
  345.     If left(A_strDir, 1) = "/" Then A_strDir = Right(A_strDir, len(A_strDir) -1)
  346.     MM_TempDir = Split(A_strDir, "/")
  347.     num = UBound(MM_TempDir)
  348.     If num > 0 Then A_strDir = MM_TempDir(num)
  349.     sql = "SELECT tplcat_id FROM " & CatTable & " WHERE tplcat_name='" & A_strDir &"'"
  350.     Set rs = Server.CreateObject("ADODB.Recordset")
  351.     rs.Open sql, m_oConn, AdOpenForwardOnly, AdLockReadOnly, adCmdText
  352.     If Not rs.EOF Then
  353.      m_Root = rs("tplcat_id")
  354.     Else
  355.      Call halt("Not Find template category " & A_strDir & " from database.")
  356.     End If
  357.     Set rs = Nothing
  358.     If Debug = 3 Then Response.Write "<p><b>Root:</b> sql = " & sql & "</p>" & VbCrLf
  359.    End If
  360.   Else
  361.    Call halt("The folder Root does not empty.")
  362.   End If
  363. End Property
  364. Public Property Get Root()
  365.   Root = m_Root
  366. End Property
  367. '####
  368. '##
  369. '## alias of Root
  370. '##
  371. Public Property Let Dir(ByVal A_strDir)
  372. '############################################################
  373.   Root = A_strDir
  374. End Property
  375. Public Property Get Dir()
  376.   Dir = Root
  377. End Property
  378. '####
  379. '## Set/Get class m_strHaltError attribute.
  380. '##
  381. '## "yes"  = the error is reported, then execution is halted.
  382. '## "no"  = errors are silently ignored.
  383. '## "report"    = the error is reported, then execution continues.
  384. '##
  385. '## usage: oTemplate.IsHalt = string A_strHalt
  386. '##
  387. '## @param     A_strHalt         new value for m_strHaltError
  388. '## @see       Halt
  389. '## @access    public
  390. '##
  391. Public Property Let IsHalt(ByVal A_strHalt)
  392. '############################################################
  393.   A_strHalt = LCase(A_strHalt)
  394.   Select Case A_strHalt
  395.    Case "yes"
  396.     m_strHaltError = "yes"
  397.    Case "no"
  398.     m_strHaltError = "no"
  399.    Case "report"
  400.     m_strHaltError = "report"
  401.   End Select
  402. End Property
  403. Public Property Get IsHalt()
  404.   IsHalt = m_strHaltError
  405. End Property
  406. '####
  407. '## Set/Get class m_strBeginTag attribute.
  408. '##
  409. '## Note: Don't conflict of HTML tag
  410. '##
  411. '## usage: oTemplate.BeginTag = string A_strTag
  412. '##
  413. '## @param     A_strTag         new value for m_strBeginTag
  414. '## @access    public
  415. '##
  416. Public Property Let BeginTag(ByVal A_strTag)
  417. '############################################################
  418.   If Debug = 4 Then Response.Write "<p><b>BeginTag:</b> BeginTag = " & A_strTag & "</p>" & VbCrLf
  419.   m_strBeginTag = A_strTag
  420. End Property
  421. Public Property Get BeginTag()
  422.   BeginTag = m_strBeginTag
  423. End Property
  424. '####
  425. '## Set/Get class m_strEndTag attribute.
  426. '##
  427. '## Note: Don't conflict of HTML tag
  428. '##
  429. '## usage: oTemplate.EndTag = string A_strTag
  430. '##
  431. '## @param     A_strTag         new value for m_strEndTag
  432. '## @access    public
  433. '##
  434. Public Property Let EndTag(ByVal A_strTag)
  435. '############################################################
  436.   If Debug = 4 Then Response.Write "<p><b>EndTag:</b> EndTag = " & A_strTag & "</p>" & VbCrLf
  437.   m_strEndTag = A_strTag
  438. End Property
  439. Public Property Get EndTag()
  440.   EndTag = m_strEndTag
  441. End Property
  442. '####
  443. '## Get class last error messages.
  444. '##
  445. '## usage: oTemplate.LastError
  446. '##
  447. '## @access    public
  448. '##
  449. Public Property Get LastError()
  450. '############################################################
  451.   LastError = m_strLastError
  452. End Property
  453. '####
  454. '## Open template database Connection object. if this Mode="db", need first open.
  455. '##
  456. '## usage: oTemplate.OpenTemplateDatabase string A_strConnString
  457. '##
  458. '## @access    public
  459. '##
  460. Public Sub OpenTemplateDatabase(ByVal A_strConnString)
  461. '############################################################
  462.   on error resume next
  463.   If Debug = 3 Then Response.Write "<p><b>OpenTemplateDatabase:</b> A_strConnString = " & A_strConnString & "</p>" & VbCrLf
  464.   if IsNull(m_oConn) Or Not IsObject(m_oConn) Then
  465.    Set m_oConn = Server.CreateObject("ADODB.Connection")
  466.    m_oConn.ConnectionString = A_strConnString
  467.    m_oConn.Open
  468.    If Err Then
  469.     err.Clear
  470.     Set m_oConn = Nothing
  471.     Call halt("Connection: Open Connection by string " & A_strConnString & " error.")
  472.    Else
  473.     m_blnConnectionState = True
  474.     m_blnNativeConnection = True
  475.    End If
  476.   End If
  477. End Sub
  478. '####
  479. '## Close template database Connection object.
  480. '##
  481. '## usage: oTemplate.CloseTemplateDatabase
  482. '##
  483. '## @access    public
  484. '##
  485. Public Sub CloseTemplateDatabase()
  486. '############################################################
  487.   if IsObject(m_oConn) Then
  488.    If Debug = 3 Then Response.Write "<p><b>CloseTemplateDatabase:</b> Close Database ... ...</p>" & VbCrLf
  489.    If m_blnNativeConnection = True Then
  490.     m_oConn.Close
  491.     Set m_oConn = Nothing
  492.    Else
  493.     Set m_oConn = Nothing
  494.    End If
  495.   End If
  496.   m_blnConnectionState = False
  497. End Sub
  498. '####
  499. '##
  500. '## @see Root
  501. '##
  502. Public Sub SetRoot(ByVal A_strDir)
  503. '############################################################
  504.   Root = A_strDir
  505. End Sub
  506. '## @same phplib::template->set_root
  507. Public Sub set_root(ByVal A_strDir)
  508.   Root = A_strDir
  509. End Sub
  510. '####
  511. '##
  512. '## @see Unknown
  513. '##
  514. Public Sub SetUnknowns(ByVal A_strUnknowns)
  515. '############################################################
  516.   Unknowns = A_strUnknowns
  517. End Sub
  518. '## @same phplib::template->set_root
  519. Public Sub set_unknowns(ByVal A_strUnknowns)
  520.   Unknowns = A_strUnknowns
  521. End Sub
  522. '####
  523. '## Defines a filename for the initial value of a variable.
  524. '##
  525. '## It may be passed either a varname and a file name as two strings or
  526. '## a hash of strings with the key being the varname and the value
  527. '## being the file name.
  528. '##
  529. '## The new mappings are stored in the object m_oFile.
  530. '## The files are not loaded yet, but only when needed.
  531. '##
  532. '##
  533. '## usage: oTemplate.SetFile A_varname, A_filename
  534. '## or
  535. '## usage: oTemplate.SetFile array(A_varname1, A_filename1 _
  536. '##           ,A_varname2, A_filename2 _
  537. '##           ,.... .... , ,,,. ,,,, ) _
  538. '##           , ""
  539. '## @see    SetFiles
  540. '## @param     A_varname      either a string containing a varname or a hash of varname/file name pairs.
  541. '## @param     A_filename     if varname is a string this is the filename otherwise filename is not required
  542. '## @access    public
  543. '##
  544. Public Sub SetFile(ByVal A_varname, ByVal A_filename)
  545. '############################################################
  546.   Dim MM_strFiles, num
  547.   If Not IsArray(A_varname) Then
  548.    If Debug = 4 Then Response.Write "<p><b>SetFile:</b> (with scalar) varname = "& A_varname &", filename = "& A_filename &"</p>" & VbCrLf
  549.    If A_filename = "" Then
  550.     Call halt("SetFile: For varname " & A_filename & " filename is empty.")
  551.     Exit Sub
  552.    End If
  553.    MM_strFiles = filename(A_filename)
  554.    m_oFile.Add A_varname, MM_strFiles
  555.   Else
  556.    Call SetFiles(A_varname)
  557.   End If
  558. End Sub
  559. '####
  560. '## Defines a multi-filename for the initial value of a variable.
  561. '##
  562. '## usage: oTemplate.SetFiles array(A_varname1, A_filename1 _
  563. '##           ,A_varname2, A_filename2 _
  564. '##           ,.... .... , ,,,. ,,,, )
  565. '## @param     array A_varname
  566. '## @access    public
  567. '## @see SetFile
  568. '##
  569. Public Sub SetFiles(ByVal A_varname)
  570. '############################################################
  571.   Dim i, num
  572.   If IsArray(A_varname) Then
  573.    num = Ubound(A_varname)
  574.    if ((num +1) mod 2) <> 0 Then
  575.     Call halt("SetFiles: For varname array's element not gemination.")
  576.     Exit Sub
  577.    Else
  578.     For i = 0 To num Step 2
  579.      Call SetFile(A_varname(i), A_varname(i+1))
  580.     Next
  581.    End If
  582.   Else
  583.    Call SetFile(A_varname, "")
  584.   End If
  585. End Sub
  586. '## @same phplib::template->set_file
  587. Public Sub set_file(ByVal A_varname, ByVal A_filename)
  588.   Call SetFile(A_varname, A_filename)
  589. End Sub
  590. '####
  591. '## A variable $parent may contain a variable block defined by:
  592. '## <!-- BEGIN A_varname --> content <!-- END A_varname -->. This function removes
  593. '## that block from $parent and replaces it with a variable reference named $name.
  594. '## The block is inserted into the varkeys and varvals hashes. If A_name is
  595. '## omitted, it is assumed to be the same as A_varname.
  596. '##
  597. '## Blocks may be nested but care must be taken to extract the blocks in order
  598. '## from the innermost block to the outermost block.
  599. '##
  600. '## usage: oTemplate.SetBlock string A_parent, string A_parent, string A_name
  601. '##
  602. '## @param     A_parent       a string containing the name of the parent variable
  603. '## @param     A_varname      a string containing the name of the block to be extracted
  604. '## @param     A_name         the name of the variable in which to store the block
  605. '## @access    public
  606. '##
  607. Public Sub SetBlock(ByVal A_parent, ByVal A_varname, ByVal A_name)
  608. '############################################################
  609.   Dim MM_String, MM_MatchString
  610.   If Debug = 4 Then Response.Write "<p><b>SetBlock:</b> parent = " & A_parent & ", varname = " & A_varname & ", name = " & A_name & "</p>" & VbCrLf
  611.   If Not loadfile(A_parent) Then
  612.    Call halt("SetBlock: unable to load " & A_parent & ".")
  613.    Exit Sub
  614.   End If
  615.   if A_name = "" Then A_name = A_varname
  616.   MM_String = GetVar(A_parent)
  617.   m_oRegExp.IgnoreCase = True
  618.   m_oRegExp.Global = True
  619.   m_oRegExp.Pattern = "<!--\s+BEGIN\s+(" & A_varname & ")\s+-->([\s\S.]*)<!--\s+END\s+\1\s+-->"
  620.   Set Matches = m_oRegExp.Execute(MM_String)
  621.   For Each Match In Matches
  622.    MM_MatchString = Match.SubMatches(1)
  623.    MM_String = m_oRegExp.Replace(MM_String, BeginTag & A_name & EndTag)
  624.    Call SetVar(A_varname,MM_MatchString)
  625.    Call SetVar(A_parent,MM_String)
  626.   Next
  627. End Sub
  628. '## @same phplib::template->set_block
  629. Public Sub set_block(ByVal A_parent, ByVal A_varname, ByVal A_name)
  630.   Call SetBlock(A_parent, A_varname, A_name)
  631. End Sub
  632. '####
  633. '## This functions sets the value of a variable.
  634. '##
  635. '## It may be called with either a varname and a value as two strings or an
  636. '## an associative array with the key being the varname and the value being
  637. '## the new variable value.
  638. '##
  639. '## The function inserts the new value of the variable into the $varkeys and
  640. '## $varvals hashes. It is not necessary for a variable to exist in these hashes
  641. '## before calling this function.
  642. '##
  643. '## usage: oTemplate.SetVar string A_varname, string A_value
  644. '## or
  645. '## usage: oTemplate.SetVar array( A_varname1, A_value1 _
  646. '##          ,A_varname2, A_value2 _
  647. '##          ,    ...   ,    ...    ) _
  648. '##          , ""     
  649. '##
  650. '## @param     A_varname      either a string containing a varname or a hash of varname/value pairs.
  651. '## @param     A_value        if A_varname is a string this contains the new value for the variable otherwise this parameter is ignored
  652. '## @access    public
  653. '##
  654. Public Sub SetVar(ByVal A_varname, ByVal A_value)
  655. '############################################################
  656.   Dim MM_varname
  657.   If Not IsArray(A_varname) Then
  658.    If A_varname <> "" Then
  659.    If Debug = 1 Then Response.Write "<b>SetVar:</b> (with scalar) <b>" & A_varname & "</b> = " & Server.HTMLEncode(A_value) & "<br>" & VbCrLf
  660.     MM_varname = varname(A_varname)
  661.     if m_oVarKeys.Exists(A_varname) Then
  662.      m_oVarKeys.Remove A_varname
  663.      m_oVarKeys.Add A_varname, MM_varname
  664.     Else
  665.      m_oVarKeys.Add A_varname, MM_varname
  666.     End If
  667.     If m_oVarVals.Exists(A_varname) Then
  668.      m_oVarVals.Remove A_varname
  669.      m_oVarVals.Add A_varname, A_value
  670.     Else
  671.      m_oVarVals.Add A_varname, A_value
  672.     End If
  673.    End If
  674.   Else
  675.    Call SetVars(A_varname)
  676.   End If
  677. End Sub
  678. '####
  679. '## usage: oTemplate.SetVar array( A_varname1, A_value1 _
  680. '##          ,A_varname2, A_value2 _
  681. '##          ,    ...   ,    ...    )
  682. '## @param     A_varname      a hash of varname/value pairs.
  683. '## @access    public
  684. '## @see       SetVar
  685. '##
  686. Public Sub SetVars(ByVal A_varname)
  687. '############################################################
  688.   Dim i, num
  689.    If IsArray(A_varname) Then
  690.     num = Ubound(A_varname)
  691.     if ((num +1) mod 2) <> 0 Then
  692.      Call halt("SetVars: For varname array's element not gemination.")
  693.      Exit Sub
  694.     Else
  695.      For i = 0 To num Step 2
  696.       Call SetVar(A_varname(i), A_varname(i+1))
  697.      Next
  698.     End If
  699.    Else
  700.     Call SetVar(A_varname, "")
  701.    End If
  702. End Sub
  703. '####
  704. '## usage: oTemplate.SetAppendVar string A_varname, string A_value
  705. '## or
  706. '## usage: oTemplate.SetAppendVar array( A_varname1, A_value1 _
  707. '##          ,A_varname2, A_value2 _
  708. '##          ,    ...   ,    ...    ) _
  709. '##          , ""
  710. '## @param     A_varname      either a string containing a varname or a hash of varname/value pairs.
  711. '## @param     A_value        if A_varname is a string this contains the new value for the variable otherwise this parameter is ignored
  712. '## @access    public
  713. '## @see    SetVar
  714. '##
  715. Public Sub SetAppendVar(ByVal A_varname, ByVal A_value)
  716. '############################################################
  717.   Dim MM_varname, MM_string
  718.   If Not IsArray(A_varname) Then
  719.    If A_varname <> "" Then
  720.     If Debug = 1 Then Response.Write "<b>SetAppendVar:</b> (with scalar) <b>" & A_varname & "</b> = " & Server.HTMLEncode(A_value) & "<br>" & VbCrLf
  721.     MM_varname = varname(A_varname)
  722.     if m_oVarKeys.Exists(A_varname) Then
  723.      m_oVarKeys.Remove A_varname
  724.      m_oVarKeys.Add A_varname, MM_varname
  725.     Else
  726.      m_oVarKeys.Add A_varname, MM_varname
  727.     End If
  728.     If m_oVarVals.Exists(A_varname) Then
  729.      MM_string = m_oVarVals.Item(A_varname) & A_value
  730.      m_oVarVals.Remove A_varname
  731.      m_oVarVals.Add A_varname, MM_string
  732.     Else
  733.      m_oVarVals.Add A_varname, A_value
  734.     End If
  735.    End If
  736.   Else
  737.    Call SetAppendVars(A_varname)
  738.   End If
  739. End Sub
  740. '####
  741. '## usage: oTemplate.SetAppendVars array( A_varname1, A_value1 _
  742. '##          ,A_varname2, A_value2 _
  743. '##          ,    ...   ,    ...    )
  744. '## @param     A_varname      a hash of varname/value pairs.
  745. '## @access    public
  746. '## @see       SetVar
  747. '##
  748. Public Sub SetAppendVars(ByVal A_varname)
  749. '############################################################
  750.   Dim i, num
  751.    If IsArray(A_varname) Then
  752.     num = Ubound(A_varname)
  753.     if ((num +1) mod 2) <> 0 Then
  754.      Call halt("SetVars: For varname array's element not gemination.")
  755.      Exit Sub
  756.     Else
  757.      For i = 0 To num Step 2
  758.       Call SetAppendVar(A_varname(i), A_varname(i+1))
  759.      Next
  760.     End If
  761.    Else
  762.     Call SetAppendVar(A_varname, "")
  763.    End If
  764. End Sub
  765. '####
  766. '##
  767. '## @same phplib::template->set_var
  768. '##
  769. Public Sub set_var(ByVal A_varname, ByVal A_value, ByVal A_append)
  770. '############################################################
  771.   If CBool(A_append) = True Then
  772.    If Not IsArray(A_varname) Then
  773.     Call SetAppendVar(A_varname, A_value)
  774.    Else
  775.     Call SetAppendVars(A_varname, A_value)
  776.    End If
  777.   Else
  778.    If Not IsArray(A_varname) Then
  779.     Call SetVar(A_varname, A_value)
  780.    Else
  781.     Call SetVars(A_varname, A_value)
  782.    End If
  783.   End If
  784. End Sub
  785. '####
  786. '## This function fills in all the variables contained within the variable named
  787. '## A_varname. The resulting value is returned as the function result and the
  788. '## original value of the variable varname is not changed. The resulting string
  789. '## is not "finished", that is, the unresolved variable name policy has not been
  790. '## applied yet.
  791. '##
  792. '## Returns: the value of the variable $varname with all variables substituted.
  793. '##
  794. '## usage: SubString(string A_varname)
  795. '##
  796. '## @param     A_varname      the name of the variable within which variables are to be substituted
  797. '## @access    public
  798. '## @return    string
  799. '##
  800. Public Function SubString(ByVal A_varname)
  801. '############################################################
  802.   Dim MM_String
  803.   If Debug = 4 Then Response.Write "<p><b>SubString:</b> varname = " & A_varname & "</p>" & VbCrLf
  804.   If Not loadfile(A_varname) Then
  805.    Call halt("SubString: unable to load " & A_varname & ".")
  806.   End If
  807.   MM_String = GetVar(A_varname)
  808.   m_oRegExp.IgnoreCase = True
  809.   m_oRegExp.Global = True
  810.   m_oRegExp.Pattern = "(" & BeginTag & ")([^ \t\r\n" & EndTag &"]+)" & EndTag
  811.   Set Matches = m_oRegExp.Execute(MM_String)
  812.   For Each Match In Matches
  813.    if m_oVarVals.Exists(Match.SubMatches(1)) Then
  814.     m_oRegExp.Pattern = Match.Value
  815.     MM_String = m_oRegExp.Replace(MM_String, m_oVarVals.Item(Match.SubMatches(1)))
  816.    End If
  817.   Next
  818.   SubString = MM_String
  819. End Function
  820. '####
  821. '##
  822. '## @same phplib::template->subst
  823. '##
  824. Public Function subst(ByVal A_varname)
  825.   subst = SubString(A_varname)
  826. End Function
  827. '####
  828. '## This is shorthand for print SubString(A_varname). See SubString for further
  829. '## details.
  830. '##
  831. '## usage: oTemplate.WriteSubString string A_varname
  832. '##
  833. '## @param     A_varname      the name of the variable within which variables are to be substituted
  834. '## @access    public
  835. '## @see       SubString
  836. '##
  837. Public Sub WriteSubString(ByVal A_varname)
  838. '############################################################
  839.   If Debug = 4 Then Response.Write "<p><b>WriteSubString:</b> varname = " & A_varname & "</p>" & VbCrLf
  840.   Response.Write SubString(A_varname)
  841. End Sub
  842. '####
  843. '##
  844. '## @same phplib::template->psubst
  845. '##
  846. Public Sub psubst(ByVal A_varname)
  847.   Call WriteSubString(A_varname)
  848. End Sub
  849. '####
  850. '## The function substitutes the values of all defined variables in the variable
  851. '## named A_varname and stores or appends the result in the variable named A_target.
  852. '##
  853. '## It may be called with either a target and a varname as two strings or a
  854. '## target as a string and an array of variable names in varname.
  855. '##
  856. '## The function inserts the new value of the variable into the oVarVeys and
  857. '## $varvals hashes. It is not necessary for a variable to exist in these hashes
  858. '## before calling this function.
  859. '##
  860. '## An optional third parameter allows the value for each varname to be appended
  861. '## to the existing target variable instead of replacing it. The default is to
  862. '## replace.
  863. '##
  864. '## If A_target and A_varname are both strings, the substituted value of the
  865. '## variable A_varname is inserted into or appended to A_target.
  866. '##
  867. '## Returns: the last value assigned to A_target.
  868. '##
  869. '## usage: oTemplate.Parse string A_target, string A_varname, boolean A_append
  870. '## usage: string = oTemplate.Parse( string A_target, string A_varname, boolean A_append )
  871. '## or
  872. '## usage: oTemplate.Parse string A_target, array(A_varname1, A_varname2, ...) , boolean A_append
  873. '## usage: string = oTemplate.Parse( string A_target, array(A_varname1, A_varname2, ...), boolean A_append)
  874. '##
  875. '## @param     A_target      a string containing the name of the variable into which substituted $varnames are to be stored
  876. '## @param     A_varname     if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted
  877. '## @param     A_append      if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced
  878. '## @access    public
  879. '## @return    string
  880. '## @see       SubString
  881. '##
  882. '## @same phplib::template->pparse
  883. '##
  884. Public Function Parse(ByVal A_target, ByVal A_varname, ByVal A_append)
  885. '############################################################
  886.   Dim MM_String, i, num
  887.   If Not IsArray(A_varname) Then
  888.    If Debug = 4 Then Response.Write "<p><b>Parse:</b> (with scalar) target = " & A_target & ", varname = " & A_varname & ", append = " & A_append & "</p>" & VbCrLf
  889.    MM_String = SubString(A_varname)
  890.    if A_append = True Then
  891.     MM_String = GetVar(A_target) & MM_String
  892.     Call SetVar(A_target, MM_String)
  893.    Else
  894.     Call SetVar(A_target, MM_String)
  895.    End If
  896.   Else
  897.    num = Ubound(A_varname)
  898.    For i = 0 To num
  899.     If Debug = 4 Then Response.Write "<p><b>Parse:</b> (with array) target = " & A_target & ", varname = " & A_varname(i) & ", append = " & A_append & "</p>" & VbCrLf
  900.     MM_String = SubString(A_varname(i))
  901.     if A_append = True Then
  902.      MM_String = GetVar(A_target) & MM_String
  903.      Call SetVar(A_target, MM_String)
  904.     Else
  905.      Call SetVar(A_target, MM_String)
  906.     End If
  907.    Next
  908.   End If
  909.   If Debug = 4 Then Response.Write "<p><b>Parse:</b> completed</p>" & VbCrLf
  910.   Parse = MM_String
  911. End Function
  912. '####
  913. '## This is shorthand for print Parse(...) and is functionally identical.
  914. '## See Parse for further details.
  915. '##
  916. '## Returns: always returns void.
  917. '##
  918. '## usage: oTemplate.Write string A_target, string A_varname
  919. '##
  920. '## @param     A_target      a string containing the name of the variable into which substituted $varnames are to be stored
  921. '## @param     A_varname     if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted
  922. '## @access    public
  923. '## @return    void
  924. '## @see       Parse
  925. '##
  926. Public Sub Write(ByVal A_target, ByVal A_varname)
  927. '############################################################
  928.   Dim MM_string
  929.   If Debug = 4 Then Response.Write "<p><b>Write:</b> passing parameters to parse...</p>" & VbCrLf
  930.   MM_string = Parse(A_target, A_varname, False)
  931.   MM_string = Finish(MM_string)
  932.   Response.Write MM_string
  933. End Sub
  934. '####
  935. '##
  936. '## @see Write
  937. '##
  938. Public Sub AppendWrite(ByVal A_target, ByVal A_varname)
  939. '############################################################
  940.   Dim MM_string
  941.   If Debug = 4 Then Response.Write "<p><b>Write:</b> passing parameters to parse...</p>" & VbCrLf
  942.   MM_string = Parse(A_target, A_varname, True)
  943.   MM_string = Finish(MM_string)
  944.   Response.Write MM_string
  945. End Sub
  946. '####
  947. '##
  948. '## @same phplib::template->pparse
  949. '##
  950. Public Sub pparse(ByVal A_target, ByVal A_varname, ByVal A_append)
  951. '############################################################
  952.   If CBool(A_append) = True Then
  953.    Call AppendWrite(A_target, A_varname)
  954.   Else
  955.    Call Write(A_target, A_varname)
  956.   End If
  957. End Sub
  958. '####
  959. '## This function returns an associative object of all defined variables with the
  960. '## name as the key and the value of the variable as the value.
  961. '##
  962. '## This is mostly useful for debugging. Also note that $this->debug can be used
  963. '## to echo all variable assignments as they occur and to trace execution.
  964. '##
  965. '## Returns: a hash of all defined variable values keyed by their names.
  966. '##
  967. '## usage: oTemplate.get_vars()
  968. '##
  969. '## @access    public
  970. '## @return    object
  971. '##
  972. Public Function GetVars()
  973. '############################################################
  974.   If Debug = 4 Then Response.Write "<p><b>GetVars:</b> constructing dictionary of vars...</p>" & VbCrLf
  975.   Set GetVars = m_oVarVals
  976. End Function
  977. '####
  978. '##
  979. '## @same phplib::template->get_vars
  980. '##
  981. Public Function get_vars()
  982.   Set get_vars = GetVars()
  983. End Function
  984. '####
  985. '## This function returns the value of the variable named by A_varname.
  986. '## If A_varname references a file and that file has not been loaded yet, the
  987. '## variable will be reported as empty.
  988. '##
  989. '## When called with an array of variable names this function will return a a
  990. '## hash of variable values keyed by their names.
  991. '##
  992. '## Returns: a string or an array containing the value of $varname.
  993. '##
  994. '## usage: GetVar(string A_varname)
  995. '## or
  996. '## usage: GetVar(array A_varname)
  997. '##
  998. '## @param     A_varname     if a string, the name the name of the variable to get the value of, or if an array a list of variables to return the value of
  999. '## @access    public
  1000. '## @return    string or object
  1001. '##
  1002. Public Function GetVar(ByVal A_varname)
  1003. '############################################################
  1004.   Dim MM_String, MM_oVars, i, num
  1005.   If Not IsArray(A_varname) Then
  1006.    'MM_String = ""
  1007.    if A_varname <> "" Then
  1008.     If m_oVarVals.Exists(A_varname) Then
  1009.      MM_String = m_oVarVals.Item(A_varname)
  1010.     End If
  1011.    End If
  1012.    If Debug = 2 Then Response.Write "<b>GetVar:</b> (with scalar) <b>" & A_varname & "</b> = " & Server.HTMLEncode(MM_String) & "<br>" & VbCrLf
  1013.    GetVar = MM_String
  1014.   Else
  1015.    Set MM_oVars = CreateObject("Scripting.Dictionary")
  1016.    num = UBound(A_varname)
  1017.    For i=0 To num
  1018.     If m_oVarVals.Exists(A_varname(i)) Then
  1019.      MM_String = m_oVarVals.Item(A_varname(i))
  1020.      MM_oVars.Add A_varname(i), MM_String
  1021.     End If
  1022.     If Debug = 2 Then Response.Write "<b>GetVar:</b> (with array) <b>" & A_varname(i) & "</b> = " & Server.HTMLEncode(MM_String) & "<br>" & VbCrLf
  1023.    Next
  1024.    Set GetVar = MM_oVars
  1025.   End If
  1026. End Function
  1027. '####
  1028. '##
  1029. '## @same phplib::template->get_var
  1030. '##
  1031. Public Function get_var(ByVal A_varname)
  1032.   If Not IsArray(A_varname) Then
  1033.    get_var = GetVar(A_varname)
  1034.   Else
  1035.    Set get_var = GetVar(A_varname)
  1036.   End If
  1037. End Function
  1038. '####
  1039. '## This functions clears the value of a variable.
  1040. '##
  1041. '## It may be called with either a varname as a string or an array with the
  1042. '## values being the varnames to be cleared.
  1043. '##
  1044. '## The function sets the value of the variable in the oVarKeys and oVarVals
  1045. '## hashes to "". It is not necessary for a variable to exist in these hashes
  1046. '## before calling this function.
  1047. '##
  1048. '##
  1049. '## usage: oTemplate.ClearVar string A_varname
  1050. '## or
  1051. '## usage: oTemplate.ClearVar array (A_varname1, A_varname2, ...)
  1052. '##
  1053. '## @param     $varname      either a string containing a varname or an array of varnames.
  1054. '## @access    public
  1055. '## @return    void
  1056. '##
  1057. Public Sub ClearVar(ByVal A_varname)
  1058. '############################################################
  1059.   Dim i, num
  1060.   If Not IsArray(A_varname) Then
  1061.    If A_varname <> "" Then
  1062.     If Debug = 1 Then Response.Write "<b>clear_var:</b> (with scalar) <b>" & A_varname & "</b><br>" & VbCrLf
  1063.     Call SetVar(A_varname, "")
  1064.    End If
  1065.   Else
  1066.    num = UBound(A_varname)
  1067.    For i=0 To num
  1068.     If Debug = 1 Then Response.Write "<b>clear_var:</b> (with array) <b>" & A_varname(i) & "</b><br>" & VbCrLf
  1069.     Call SetVar(A_varname(i), "")
  1070.    Next
  1071.   End If
  1072. End Sub
  1073. '####
  1074. '##
  1075. '## @same phplib::template->clear_var
  1076. '##
  1077. Public Sub clear_var(ByVal A_varname)
  1078.   Call ClearVar(A_varname)
  1079. End Sub
  1080. '####
  1081. '## This functions unsets a variable completely.
  1082. '##
  1083. '## It may be called with either a varname as a string or an array with the
  1084. '## values being the varnames to be cleared.
  1085. '##
  1086. '## The function removes the variable from the oVarKeys and oVarVals hashes.
  1087. '## It is not necessary for a variable to exist in these hashes before calling
  1088. '## this function.
  1089. '##
  1090. '##
  1091. '## usage: oTemplate.unSetVar string A_varname
  1092. '## or
  1093. '## usage: oTemplate.unSetVar array(A_varname1, A_varname2, ...)
  1094. '##
  1095. '## @param     A_varname      either a string containing a varname or an array of varnames.
  1096. '## @access    public
  1097. '##
  1098. Public Sub unSetVar(ByVal A_varname)
  1099. '############################################################
  1100.   Dim i, num
  1101.   If Not IsArray(A_varname) Then
  1102.    If A_varname <> "" Then
  1103.     If Debug = 1 Then Response.Write "<b>unSetVar:</b> (with scalar) <b>" & A_varname & "</b><br>" & VbCrLf
  1104.     If m_oVarKeys.Exists(A_varname) Then
  1105.      m_oVarKeys.Remove A_varname
  1106.     End If
  1107.     If m_oVarVals.Exists(A_varname) Then
  1108.      m_oVarVals.Remove A_varname
  1109.     End If
  1110.    End If
  1111.   Else
  1112.    num = UBound(A_varname)
  1113.    For i=0 To num
  1114.     If A_varname(i) <> "" Then
  1115.      If Debug = 1 Then Response.Write "<b>unSetVar:</b> (with array) <b>" & A_varname & "</b><br>" & VbCrLf
  1116.      If m_oVarKeys.Exists(A_varname(i)) Then
  1117.       m_oVarKeys.Remove A_varname(i)
  1118.      End If
  1119.      If m_oVarVals.Exists(A_varname(i)) Then
  1120.       m_oVarVals.Remove A_varname(i)
  1121.      End If
  1122.     End If
  1123.    Next
  1124.   End If
  1125. End Sub
  1126. '####
  1127. '##
  1128. '## @same phplib::template->unset_var
  1129. '##
  1130. Public Sub unset_var(ByVal A_varname)
  1131.   Call unSetVar(A_varname)
  1132. End Sub
  1133. '####
  1134. '## This function returns a hash of unresolved variable names in A_varname, keyed
  1135. '## by their names.
  1136. '##
  1137. '## Returns: a hash of varname/varname pairs or false on error.
  1138. '##
  1139. '## usage: GetUndefined(string A_varname)
  1140. '##
  1141. '## @param     A_varname     a string containing the name the name of the variable to scan for unresolved variables
  1142. '## @access    public
  1143. '## @return    array
  1144. '##
  1145. Public Function GetUndefined(ByVal A_varname)
  1146. '############################################################
  1147.   Dim MM_String, MM_result
  1148.   If Debug = 4 Then Response.Write "<p><b>GetUndefined:</b> varname = " & A_varname & "</p>" & VbCrLf
  1149.   If Not loadfile(A_varname) Then
  1150.    Call halt("get_undefined: unable to load " & A_varname & ".")
  1151.    GetUndefined = False
  1152.    Exit Function
  1153.   End If
  1154.   MM_String = GetVar(A_varname)
  1155.   'Set MM_result = CreateObject("Scripting.Dictionary")
  1156.   m_oRegExp.IgnoreCase = True
  1157.   m_oRegExp.Global = True
  1158.   m_oRegExp.Pattern = "(" & BeginTag & ")([^ \t\r\n" & EndTag &"]+)" & EndTag
  1159.   Set Matches = m_oRegExp.Execute(MM_String)
  1160.   i = 0
  1161.   For Each Match In Matches
  1162.    if Not m_oVarVals.Exists(Match.SubMatches(1)) Then
  1163.     If Debug = 4 Then Response.Write "<p><b>get_undefined:</b> undefined: " & SubMatches(1) & "</p>" & VbCrLf
  1164.     'MM_result.Add Match.SubMatches(1), Match.SubMatches(1)
  1165.     MM_result(i) = Match.SubMatches(1)
  1166.     i = i + 1
  1167.    End If
  1168.   Next
  1169.   'if MM_result.Count > 0 Then
  1170.   ' Set GetUndefined = MM_result
  1171.   If IsArray(MM_result) Then
  1172.    GetUndefined = MM_result
  1173.   Else
  1174.    GetUndefined = False
  1175.   End If
  1176. End Function
  1177. '####
  1178. '##
  1179. '## @same phplib::template->get_undefined
  1180. '##
  1181. Public Function get_undefined(ByVal A_varname)
  1182. '############################################################
  1183.   get_undefined = GetUndefined
  1184. End Function
  1185. '####
  1186. '## This function returns the finished version of $str. That is, the policy
  1187. '## regarding unresolved variable names will be applied to $str.
  1188. '##
  1189. '## Returns: a finished string derived from A_String and unknowns.
  1190. '##
  1191. '## usage: Finish(string A_String)
  1192. '##
  1193. '## @param     A_String         a string to which to apply the unresolved variable policy
  1194. '## @access    public
  1195. '## @return    string
  1196. '## @see       Unknowns, SetUnknowns, set_unknowns
  1197. '##
  1198. Public Function Finish(ByVal A_String)
  1199. '############################################################
  1200.   Dim MM_String
  1201.   Select Case Unknowns
  1202.    case "keep"
  1203.     MM_String = A_String
  1204.    case "remove"
  1205.     m_oRegExp.IgnoreCase = True
  1206.     m_oRegExp.Global = True
  1207.     m_oRegExp.Pattern = "(" & BeginTag & ")([^ \t\r\n" & EndTag &"]+)" & EndTag
  1208.     MM_String = m_oRegExp.Replace(A_String, "")
  1209.    case "comment"
  1210.     m_oRegExp.IgnoreCase = True
  1211.     m_oRegExp.Global = True
  1212.     m_oRegExp.Pattern = "(" & BeginTag & ")([^ \t\r\n" & EndTag &"]+)" & EndTag
  1213.     Set Matches = m_oRegExp.Execute(A_String)
  1214.     For Each Match In Matches
  1215.      MM_String = m_oRegExp.Replace(A_String, "<!-- Template variable " & Match.SubMatches(1) &" undefined -->")
  1216.     Next
  1217.   End Select
  1218.   Finish = MM_String
  1219. End Function
  1220. '####
  1221. '## This function returns the finished version of the value of the variable named
  1222. '## by $varname. That is, the policy regarding unresolved variable names will be
  1223. '## applied to the variable A_varname and the result returned.
  1224. '##
  1225. '## Returns: a finished string derived from the variable A_varname.
  1226. '##
  1227. '## usage: oTemplate.GetVariable(string A_varname)
  1228. '##
  1229. '## @param     A_varname     a string containing the name of the variable to finish
  1230. '## @access    public
  1231. '## @return    string
  1232. '## @see       SetUnknowns
  1233. '## @see       Finish
  1234. '##
  1235. Public Function GetVariable(ByVal A_varname)
  1236. '############################################################
  1237.   GetVariable = Finish(GetVar(A_varname))
  1238. End Function
  1239. 'Public Function get(ByVal A_varname)
  1240. '冲突不支持
  1241. 'End Function
  1242. '####
  1243. '## This function prints the finished version of the value of the variable named
  1244. '## by $varname. That is, the policy regarding unresolved variable names will be
  1245. '## applied to the variable A_varname then it will be printed.
  1246. '##
  1247. '## usage: oTemplate.WriteVariable string A_varname
  1248. '##
  1249. '## @param     A_varname     a string containing the name of the variable to finish and print
  1250. '## @access    public
  1251. '## @see       SetUnknowns
  1252. '## @see       Finish
  1253. '##
  1254. Public Sub WriteVariable(ByVal A_varname)
  1255. '############################################################
  1256.   Response.Write Finish(GetVal(A_varname))
  1257. End Sub
  1258. '####
  1259. '##
  1260. '## @see WriteVariable
  1261. '## @same phplib::template->p
  1262. '##
  1263. Public Sub p(ByVal A_varname)
  1264.   Call WriteVariable(A_varname)
  1265. End Sub
  1266. '####
  1267. '## When called with a relative pathname, this function will return the pathname
  1268. '## with Root prepended. Absolute pathnames are returned unchanged.
  1269. '##
  1270. '## Returns: a string containing an absolute pathname.
  1271. '##
  1272. '## usage: filename(string A_filename)
  1273. '##
  1274. '## @param     A_filename    a string containing a filename
  1275. '## @access    private
  1276. '## @return    string
  1277. '## @see       Root, SetRoot
  1278. '##
  1279. '## @same phplib::template->filename
  1280. '##
  1281. Private Function filename(ByVal A_filename)
  1282. '############################################################
  1283.   Dim MM_FSO, MM_filename, MM_TempFilename, rs, sql
  1284.   If Debug = 4 Then Response.Write "<p><b>filename:</b> filename = " & A_filename & "</p>" & VbCrLf
  1285.   If Mode = "file" Then
  1286.    Set MM_FSO = CreateObject("Scripting.FileSystemObject")
  1287.    If Left(A_filename, 1) = "/" Then
  1288.     A_filename = Right(A_filename, len(A_filename) - 1)
  1289.    End If
  1290.    A_filename = Root & A_filename
  1291.    A_filename = Server.MapPath(A_filename)
  1292.    If Not MM_FSO.FileExists(A_filename) Then
  1293.     Call halt("filename: file " & A_filename & " does not exist.")
  1294.    Else
  1295.     MM_filename = A_filename
  1296.    End If
  1297.   ElseIf Mode = "db" Then
  1298.     A_filename = Split(A_filename, ".")
  1299.     MM_TempFilename = A_filename(0)
  1300.     sql = "SELECT tpldata_id FROM " & DataTable & " WHERE tplcat_id =" & Root &" AND tpldata_name='" & MM_TempFilename &"'"
  1301.     Set rs = Server.CreateObject("ADODB.Recordset")
  1302.     rs.Open sql, m_oConn, AdOpenForwardOnly, AdLockReadOnly, adCmdText
  1303.     If rs.EOF Then
  1304.      Call halt("filename: file " & MM_TempFilename & " does not exist.")
  1305.     Else
  1306.      MM_filename = rs("tpldata_id")
  1307.     End If
  1308.     Set rs = Nothing
  1309.     If Debug = 3 Then Response.Write "<p><b>filename:</b> sql = " & sql & "</p>" & VbCrLf
  1310.   End If
  1311.   filename = MM_filename
  1312. End Function
  1313. '####
  1314. '## If a variable's value is undefined and the variable has a filename stored in
  1315. '## ofile.Item(A_varname) then the backing file will be loaded and the file's
  1316. '## contents will be assigned as the variable's value.
  1317. '##
  1318. '## Note that the behaviour of this function changed slightly after the 7.2d
  1319. '## release. Where previously a variable was reloaded from file if the value
  1320. '## was empty, now this is not done. This allows a variable to be loaded then
  1321. '## set to "", and also prevents attempts to load empty variables. Files are
  1322. '## now only loaded if oVarVals.Item(A_varname) is unset.
  1323. '##
  1324. '## Returns: true on success, false on error.
  1325. '##
  1326. '## usage: loadfile(string A_varname)
  1327. '##
  1328. '## @param     A_varname    a string containing the name of a variable to load
  1329. '## @access    private
  1330. '## @return    boolean
  1331. '## @see       SetFile, SetFiles
  1332. '##
  1333. '## @same phplib::template->loadfile
  1334. '##
  1335. Private Function loadfile(ByVal A_varname)
  1336. '############################################################
  1337.   Dim MM_FSO, MM_oFile, MM_filename, MM_FileSting, MM_bool
  1338.   If Debug = 4 Then Response.Write "<p><b>loadfile:</b> varname = " & A_varname & "</p>" & VbCrLf
  1339.   MM_bool = true
  1340.   If Not m_oFile.Exists(A_varname) Then
  1341.    loadfile = MM_bool
  1342.    If Debug = 4 Then Response.Write "<p><b>loadfile:</b> varname " & A_varname & " does not reference a file</p>" & VbCrLf
  1343.    Exit Function
  1344.   End If
  1345.   If m_oVarVals.Exists(A_varname) Then
  1346.    loadfile = MM_bool
  1347.    If Debug = 4 Then Response.Write "<p><b>loadfile:</b> varname " & A_varname & " is already loaded</p>" & VbCrLf
  1348.    Exit Function
  1349.   End If
  1350.   MM_filename = m_oFile.Item(A_varname)
  1351.   If Mode = "file" Then
  1352.    Set MM_FSO = CreateObject("Scripting.FileSystemObject")
  1353.    Set MM_oFile = MM_FSO.OpenTextFile(MM_filename)
  1354.    MM_FileSting = MM_oFile.ReadAll
  1355.    'MM_FileSting = Trim(MM_FileSting)
  1356.    If MM_FileSting = "" Then
  1357.     MM_bool = false
  1358.     Call halt("loadfile: While loading " & A_varname & ", " & MM_filename & " does not exist or is empty.")
  1359.    Else
  1360.     If Debug = 4 Then Response.Write "<b>loadfile:</b> loaded " & MM_filename & " into " & A_varname & "<br>" & VbCrLf
  1361.     Call SetVar(A_varname, MM_FileSting)
  1362.    End If
  1363.    MM_oFile.Close
  1364.    Set MM_oFile = Nothing
  1365.    set FSO = nothing
  1366.   ElseIf Mode = "db" Then
  1367.     sql = "SELECT tpldata_text FROM " & DataTable & " WHERE tpldata_id =" & MM_filename
  1368.     Set rs = Server.CreateObject("ADODB.Recordset")
  1369.     rs.Open sql, m_oConn, AdOpenForwardOnly, AdLockReadOnly, adCmdText
  1370.     If rs.EOF Then
  1371.      MM_bool = false
  1372.      Call halt("filename: file " & MM_TempFilename & " does not exist.")
  1373.     Else
  1374.      MM_FileSting = rs("tpldata_text")
  1375.      Call SetVar(A_varname, MM_FileSting)
  1376.     End If
  1377.     Set rs = Nothing
  1378.     If Debug = 3 Then Response.Write "<p><b>loadfile:</b> sql = " & sql & "</p>" & VbCrLf
  1379.   End If
  1380.   loadfile = MM_bool
  1381. End Function
  1382. '####
  1383. '## This function will construct a regexp for a given variable name with any
  1384. '## special chars quoted.
  1385. '##
  1386. '## Returns: a string containing an escaped variable name.
  1387. '##
  1388. '## usage: varname(string A_varname)
  1389. '##
  1390. '## @param     A_varname    a string containing a variable name
  1391. '## @access    private
  1392. '## @return    string
  1393. '## @same phplib::template->varname
  1394. '##
  1395. Private Function varname(ByVal A_varname)
  1396. '############################################################
  1397.   varname = BeginTag & A_varname & EndTag
  1398. End Function
  1399. '####
  1400. '## This function is called whenever an error occurs and will handle the error
  1401. '## according to the policy defined in IsHalt. Additionally the
  1402. '## error message will be saved in m_strLastError.
  1403. '##
  1404. '## Returns: always returns false.
  1405. '##
  1406. '## usage: halt(string A_message)
  1407. '##
  1408. '## @param     $msg         a string containing an error message
  1409. '## @access    private
  1410. '## @return    void
  1411. '## @see       IsHalt
  1412. '##
  1413. Private Sub halt(ByVal A_message)
  1414. '############################################################
  1415.   m_strLastError = A_message
  1416.   If IsHalt <> "no" Then Call haltmsg(A_message)
  1417.   If IsHalt = "yes" Then
  1418.    Response.Write "<b>Halted.</b>"
  1419.    Response.End
  1420.   End If
  1421. End Sub
  1422. '####
  1423. '## This function prints an error message.
  1424. '## It can be overridden by your subclass of Template. It will be called with an
  1425. '## error message to display.
  1426. '##
  1427. '## usage: haltmsg(string A_message)
  1428. '##
  1429. '## @param     A_message         a string containing the error message to display
  1430. '## @access    public
  1431. '## @return    void
  1432. '## @see       halt
  1433. '##
  1434. Public Sub haltmsg(ByVal A_message)
  1435. '############################################################
  1436.   Response.Write "<b>Template Error:</b>" & A_message & "<br>"
  1437. End Sub
  1438. '####
  1439. '## Class constructor, set class default attributes, you can change it
  1440. '## @see Property Let Debug
  1441. '## @see Property Let Mode
  1442. '## @see Property Let CatTable
  1443. '## @see Property Let DataTable
  1444. '## @see Property Let Unknown
  1445. '## @see Property Let IsHalt
  1446. '## @see Property Let BeginTag
  1447. '## @see Property Let EndTag
  1448. '####
  1449. Private Sub class_Initialize
  1450.   Debug = 0
  1451.   Mode = "file"
  1452.   CatTable = "TplCat"
  1453.   DataTable = "TplData"
  1454.   Unknowns = "remove"
  1455.   IsHalt = "yes"
  1456.   m_strLastError = ""
  1457.   BeginTag = "{"
  1458.   EndTag = "}"
  1459.   m_Root = "templates/"
  1460.   Set m_oFile = CreateObject("Scripting.Dictionary")
  1461.   Set m_oVarKeys = CreateObject("Scripting.Dictionary")
  1462.   Set m_oVarVals = CreateObject("Scripting.Dictionary")
  1463.   Set m_oRegExp = New RegExp
  1464.   m_blnConnectionState = False
  1465.   m_strName = "aspTemplate"
  1466.   m_strVersion = "2.0.0"
  1467.   If Debug = 4 Then Response.Write "<p><b>Template:</b> root = " & m_Root & ", unknowns = " & Unknowns & "</p>" & VbCrLf
  1468. End Sub
  1469. '####
  1470. '## Class destructor, free memory.
  1471. '####
  1472. Private Sub class_Terminate
  1473.   Set m_oFile = Nothing
  1474.   Set m_oVarKeys = Nothing
  1475.   Set m_oVarVals = Nothing
  1476.   Set m_oRegExp = Nothing
  1477.   Call CloseTemplateDatabase()
  1478. End Sub
  1479. End Class
  1480. %>
复制代码
返回列表