(精品)星星模板类
<%
'****************************************************************
'* 星星模板类(VBS 版) Ver 2.0 Build 31010
'*
'* 文件名称:xxtemplate_vbs.asp
'*
'* 版权所有: 星星(pure_xu@126.net)
'*
'* 程序制作: 星星(pure_xu@126.net)
'*
'* 主页地址: http://www.stnjx.com 汕头南极星
'*
'* 论坛地址: http://www.stnjx.com/bbs/
'*****************************************************************
'* 模板类方法:
'* Load(sName, sFile) —— 载入文件
'* sName:文件的变量名
'* sFile:文件名(服务器端要先用 Server.MapPath 转换为绝对路径)
'* 一个模板类可以载入多个文件
'* Split(sName) —— 分离模板内的子模板
'* sName:要分离的文件变量名,或包含有孙模板的子模板名
'* 子模板以“<!--#TPLDEF[\s]+(\w+)[\s]*-->”格式开始
'* 以“<!--#TPLEND[\s]+(\w+)[\s]*-->”格式结束,可以嵌套
'* 子模板一般以实现局部代码循环和模板内的参数定义
'* Add(sName, sVal) —— 添加变量
'* sName:变量名
'* sVal :变量值
'* Parse(sName) —— 处理模板
'* sName:要处理的文件变量名或子模板变量名
'* 模板处理时将会处理模板里通过“{\w+}”形式出现的内容
'* 模板类属性
'* Version —— 版本号
'* TPL_Value —— 自定变量集合
'* 自定变量集合以数组形式保存
'* 提取时以 模板类.TPL_Value(变量名) 方式提取
Class Template_Class
Public Version, TPL_Value
Private Sub Class_Initialize
Version = "星星模板类(VBS 版) Ver 2.0 Build 31010"
Set TPL_Value = Server.CreateObject("Scripting.Dictionary")
TPL_Value.CompareMode = 1
TPL_Value.RemoveAll
End Sub
Private Sub Class_Terminate
TPL_Value.RemoveAll
Set TPL_Value = Nothing
End Sub
'模板加载
Public Sub Load(sName, sFile)
On Error Resume Next
Dim oStream
Set oStream = Server.CreateObject("Adodb.Stream")
oStream.Type = 2
oStream.Mode = 3
oStream.Open
oStream.LoadFromFile(sFile)
oStream.CharSet = "UTF-8"
oStream.Position = 2
TPL_Value.Add sName, oStream.ReadText
oStream.Close
Set oStream = Nothing
If Err.Number <> 0 Then
Response.Write Err.Description
Err.Clear
Exit Sub
End If
End Sub
'模板分离
Public Sub Split(sName)
If Not TPL_Value.Exists(sName) Or IsNull(TPL_Value(sName)) Then Exit Sub
Dim aArr, oRE, oReRE, sTemp, sVar, sVal
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = False
oRE.MultiLine = True
Set oReRE = New RegExp
oReRE.IgnoreCase = True
oReRE.Global = False
oReRE.MultiLine = True
oRE.Pattern = "<!--#TPLDEF[\s]+(\w+)[\s]*-->(.|\n)*<!--#TPLEND[\s]+\1[\s]*-->"
Do While oRE.Test(TPL_Value(sName))
Set aArr = oRE.Execute(TPL_Value(sName))
sTemp = aArr(0)
oReRE.Pattern = "<!--#TPLDEF[\s]+(\w+)[\s]*-->((.|\n)*)<!--#TPLEND[\s]+\1[\s]*-->"
sVar = oReRE.Replace(sTemp, "$1")
sTemp = aArr(0)
oReRE.Pattern = "<!--#TPLDEF[\s]+(\w+)[\s]*-->((.|\n)*)<!--#TPLEND[\s]+\1[\s]*-->"
sVal = oReRE.Replace(sTemp, "$2")
TPL_Value.Add sVar,sVal
TPL_Value(sName) = oRE.Replace(TPL_Value(sName), "{" & sVar & "}")
Call Split(sVar)
Set aArr = Nothing
Loop
Set oReRE = Nothing
Set oRE = Nothing
End Sub
'添加变量
Public Sub Add(sName, sVal)
TPL_Value.Add sName,sVal
End Sub
'模板处理
Public Function Parse(sName)
If Not TPL_Value.Exists(sName) Or IsNull(TPL_Value(sName)) Then Exit Function
Dim aArr, oRE, sKey, nLoop, sTPL, sVal
sTPL = TPL_Value(sName)
Set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
oRE.Pattern = "{\w+}"
Set aArr = oRE.Execute(sTPL)
If Not IsNull(aArr) Then
For nLoop = 0 To aArr.Count - 1
sVal = aArr(nLoop)
sKey = Mid(sVal, 2, Len(sVal) - 2)
oRE.Pattern = sVal
sTPL = oRE.Replace(sTPL, TPL_Value(sKey))
Next
End If
Parse = sTPL
Set aArr = Nothing
Set oRE = Nothing
End Function
End Class
%>