返回列表 发帖

[转载] ASP模板类[实现一维循环和二维循环,可以从文件、数据库、变量取摸板]

  1. <%
  2. '=========================================================
  3. ' File: class_template.asp
  4. ' Version:1.0
  5. ' Date: 2004-5-7
  6. ' Script Written by R.H
  7. ' Description: ASP Template Class
  8. '=========================================================
  9. ' Copyright (C) 2004 Interflower Studios. All rights reserved.
  10. ' Web: http://www.interflower.cn
  11. ' Need help? Contact: ranhuan@msn.com
  12. '=========================================================
  13. '=========================================================
  14. '模板中替换的部分用{{%}}表示
  15. '模板中的循环用<!-- BEGIN % -->开始 <!-- END % -->结束 支持一次嵌套

  16. Class Template
  17. Private tmp
  18. Private tpl_dir, tpl, tpl_blk
  19. Private var_list, blk_list, blk_var_list
  20. Private re, match, matchs
  21. Private Sub class_Initialize
  22. sql = ""
  23. tpl_dir = "templates/"
  24. tpl = ""
  25. blk=""
  26. Set var_list = Server.CreateObject("Scripting.Dictionary")
  27. Set blk_list = Server.CreateObject("Scripting.Dictionary")
  28. Set blk_var_list = Server.CreateObject("Scripting.Dictionary")
  29. Set re = New RegExp
  30. End Sub
  31. '取得主体模板
  32. '========================
  33. '从变量取出
  34. Public Sub SetTpl(tplvar)
  35. tpl = tplvar
  36. End Sub
  37. ' 从DB中取出,自己修改sql语句
  38. Public Sub SetTplDb(tplname)
  39. Dim sql, rs
  40. Set rs = Server.CreateObject("ADODB.RecordSet")
  41. sql = "SELECT content FROM templates WHERE name = '"&tplname&"'"
  42. rs.Open sql,conn,1,1
  43. If rs.RecordCount <> 1 Then
  44.   Response.Write("数据库错误!<br>")
  45.   Response.End()
  46. End If
  47. tpl = rs("content")
  48. rs.Close
  49. Set rs = Nothing
  50. End Sub
  51. '从文件取出
  52. Public Sub SetTplFile(tplfile)
  53. Dim FSO, oFile
  54. Set FSO = Server.Createobject("Scripting.FileSystemObject")
  55. If FSO.FileExists(Server.Mappath(tpl_dir & tplfile)) then
  56.   Set oFile = FSO.OpenTextFile(Server.Mappath(tpl_dir & tplfile))
  57.   tpl = oFile.ReadAll
  58.   oFile.Close
  59.   Set oFile = Nothing
  60. Else
  61.   Response.Write "模板文件不存在!<br>"
  62. End if
  63. Set FSO = nothing
  64. End Sub
  65. '取得区块模板
  66. '========================
  67. '从变量取出
  68. Public sub SetBlk(blkname, tplvar)
  69. re.IgnoreCase = True
  70. re.Global = True
  71. re.Pattern = {{ & blkname & }}
  72. tpl = re.Replace(tpl, tplvar)
  73. rs.Close
  74. End Sub
  75. '从数据库取出
  76. Public sub SetBlkDb(blkname, tplname)
  77. Dim sql, rs
  78. Set rs = Server.CreateObject("ADODB.RecordSet")
  79. sql = "SELECT * FROM templates WHERE name = '"&tplname&"'"
  80. rs.Open sql,conn,1,1
  81. tmp = rs("content")
  82. rs.Close
  83. SetBlk blkname, tmp
  84. set rs = Nothing
  85. End Sub
  86. '从文件取出
  87. Public sub SetBlkFile(blkname, tplfile)
  88. Dim FSO, oFile
  89. Set FSO = createobject("Scripting.FileSystemObject")
  90. If FSO.FileExists(server.mappath(tpl_dir &tplfile)) Then
  91.   Set oFile = FSO.OpenTextFile(Server.MapPath(tpl_dir &tplfile))
  92.   tmp = oFile.ReadAl
  93.   SetBlock blkname, tmp
  94.   oFile.Close
  95.   set oFile = Nothing
  96. Else
  97.   Response.Write "区块模板文件不存在!<br>"
  98. End If
  99. Set FSO = Nothing
  100. End Sub
  101. '设置变量替换值
  102. '========================
  103. '简单替换
  104. '已定义var_list是一个Scripting.Dictionary对象
  105. Public Sub SetVar(sName, sValue)
  106. If var_list.Exists(sName) then
  107.   var_list.Remove sName
  108.   var_list.Add sName, sValue
  109. Else
  110.   var_list.Add sName, sValue
  111. End if
  112. End Sub
  113. '简单替换 追加数据
  114. Public Sub AppendVar(sName, sValue)
  115. If var_list.Exists(sName) then
  116.   tmp = var_list.Item(sName) & sValue
  117.   var_list.Remove sName
  118.   var_list.Add sName, tmp
  119. Else
  120.   var_list.Add sName, sValue
  121. End If
  122. End Sub
  123. '循环替换
  124. '========================
  125. '一维循环开始
  126. Public Sub UdBlk(BlkName)
  127. tpl_blk = BlkName
  128. re.IgnoreCase = True
  129. re.Global = True
  130. re.Pattern = "<!--\s+BEGIN\s+(" & BlkName & ")\s+-->([\s\S.]*)<!--\s+END\s+\1\s+-->"
  131. Set Matches = re.Execute(tpl)
  132. If Matches.Count > 0 Then
  133. Set match = Matches
  134. For Each match In Matches
  135. blk_list.Add BlkName, match.SubMatches(1)
  136. var_list.Add BlkName, ""
  137. tpl = re.Replace(tpl, "{{"&BlkName&"}}")
  138. next
  139. Else
  140. Response.Write "Block " & BlkName & " does not exists!"
  141. End If
  142. end sub
  143. '一维循环结束
  144. Public Sub PsBlk(BlkName)
  145. tmp = blk_list.Item(BlkName)
  146. re.IgnoreCase = True
  147. re.Global = True
  148. re.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
  149. Set Matches = re.Execute(tmp)
  150. for each match in Matches
  151. if blk_var_list.Exists(match.SubMatches(1)) then
  152. re.Pattern = match.value
  153. tmp = re.Replace(tmp, blk_var_list.Item(match.SubMatches(1)))
  154. end if
  155. next
  156. tmp = var_list.Item(BlkName) & tmp
  157. var_list.Remove BlkName
  158. var_list.Add BlkName, tmp
  159. blk_var_list.RemoveAll
  160. End Sub
  161. '二维循环开始
  162. Public Sub UdBlk2(BlkName)
  163. tmp = blk_list.Item(tpl_blk)
  164. re.IgnoreCase = True
  165. re.Global = True
  166. re.Pattern = "<!--\s+BEGIN\s+(" & BlkName & ")\s+-->([\s\S.]*)<!--\s+END\s+\1\s+-->"
  167. Set Matches = re.Execute(tmp)
  168. If Matches.Count > 0 Then
  169. Set match = Matches
  170. For Each match In Matches
  171. blk_list.Add BlkName, match.SubMatches(1)
  172. 'response.Write match.SubMatches(1)
  173. blk_var_list.Add BlkName, ""
  174. tmp = re.Replace(tmp, "{{"&BlkName&"}}")
  175. blk_list.ReMove tpl_blk
  176. blk_list.Add tpl_blk, tmp
  177. next
  178. Else
  179. Response.Write "Block " & BlkName & " does not exists!"
  180. End If
  181. end sub
  182. '二维循环结束
  183. Public Sub PsBlk2(BlkName)
  184. tmp = blk_list.Item(BlkName)
  185. re.IgnoreCase = True
  186. re.Global = True
  187. re.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
  188. Set Matches = re.Execute(tmp)
  189. for each match in Matches
  190. if blk_var_list.Exists(match.SubMatches(1)) then
  191. re.Pattern = match.value
  192. tmp = re.Replace(tmp, blk_var_list.Item(match.SubMatches(1)))
  193. end if
  194. next
  195. tmp = blk_var_list.Item(BlkName) & tmp
  196. blk_var_list.RemoveAll
  197. blk_var_list.Add BlkName, tmp
  198. End Sub
  199. '循环中的替换
  200. Public Sub SetBlkVar(s, v)
  201. If blk_var_list.Exists(s) then
  202. blk_var_list.Remove s
  203. blk_var_list.Add s, v
  204. Else
  205. blk_var_list.Add s, v
  206. End if
  207. End Sub
  208. '解析模板和输出内容
  209. '========================
  210. '执行解析
  211. '可以通过下面的过程取得网页内容,结合fso可以生成静态页面
  212. Public Property GetTpl
  213. re.IgnoreCase = True
  214. re.Global = True
  215. re.Pattern = "(" & {{ & ")([^}]+)" & }}
  216. Set Matches = re.Execute(tpl)
  217. for each match in Matches
  218. if var_list.Exists(match.SubMatches(1)) then
  219. re.Pattern = match.Value
  220. tpl = re.Replace(tpl, var_list.Item(match.SubMatches(1)))
  221. end if
  222. next
  223. GetTpl = tpl
  224. End Property
  225. '输出内容
  226. Public Sub Parse
  227. Response.Write tpl
  228. End Sub
  229. End Class
  230. %>
复制代码
天行健,君子以自强不息
地势坤,君子以厚德载物
黑色海岸线欢迎您

QQ群:7212260
致力于探索WEB技术精髓:http://www.bitechcn.com
点这里加我!

返回列表 回复 发帖