Public Function get_file()
get_file = m_FileName
End Function
'' Public Property Let File(handle, filename)
'' set_file handle, filename
'' End Property
'' Public Property Get File()
'' File = m_FileName
'' End Property
''设置对未指定的标记的处理方式,有keep、remove、comment三种
Public Sub set_unknowns(ByVal unknowns)
m_Unknowns = unknowns
End Sub
Public Function get_unknowns()
get_unknowns = m_Unknowns
End Function
Public Property Let Unknowns(ByVal unknown)
m_Unknowns = unknown
End Property
Public Property Get Unknowns()
Unknowns = m_Unknowns
End Property
Public Sub set_block(ByVal Parent, ByVal BlockTag, ByVal Name)
Dim Matches
m_RegExp.Pattern = "<!--\s+BEGIN " & BlockTag & "\s+-->([\s\S.]*)<!--\s+END " & BlockTag & "\s+-->"
If Not m_BlockList.Exists(Parent) Then ShowError("未指定的块标记" & Parent)
set Matches = m_RegExp.Execute(m_BlockList.Item(Parent))
For Each Match In Matches
m_BlockList.Add BlockTag, Match.SubMatches(0)
m_BlockList.Item(Parent) = Replace(m_BlockList.Item(Parent), Match.Value, "{" & Name & "}")
Next
set Matches = nothing
End Sub
Public Sub set_var(ByVal Name, ByVal Value, ByVal Append)
Dim Val
If IsNull(Value) Then Val = "" Else Val = Value
If m_ValueList.Exists(Name) Then
If Append Then m_ValueList.Item(Name) = m_ValueList.Item(Name) & Val _
Else m_ValueList.Item(Name) = Val
Else
m_ValueList.Add Name, Value
End If
End Sub
Public Sub unset_var(ByVal Name)
If m_ValueList.Exists(Name) Then m_ValueList.Remove(Name)
End Sub
Private Function InstanceValue(ByVal BlockTag)
Dim keys, i
InstanceValue = m_BlockList.Item(BlockTag)
keys = m_ValueList.Keys
For i=0 To m_ValueList.Count-1
InstanceValue = Replace(InstanceValue, "{" & keys(i) & "}", m_ValueList.Item(keys(i)))
Next
End Function
Public Sub parse(ByVal Name, ByVal BlockTag, ByVal Append)
If Not m_BlockList.Exists(BlockTag) Then ShowError("未指定的 块标记" & Parent)
If m_ValueList.Exists(Name) Then
If Append Then m_ValueList.Item(Name) = m_ValueList.Item(Name) & InstanceValue(BlockTag) _
Else m_ValueList.Item(Name) = InstanceValue(BlockTag)
Else
m_ValueList.Add Name, InstanceValue(BlockTag)
End If
End Sub
Private Function finish(ByVal content)
Select Case m_Unknowns
Case "keep" finish = content
Case "remove"
m_RegExp.Pattern = "\{[^ \t\r\n}]+\}"
finish = m_RegExp.Replace(content, "")
Case "comment"
m_RegExp.Pattern = "\{([^ \t\r\n}]+)\}"
finish = m_RegExp.Replace(content, "<!-- Template Variable $1 undefined -->")
Case Else finish = content
End Select
End Function
Public Sub p(ByVal Name)
If Not m_ValueList.Exists(Name) Then ShowError("不存在的标记" & Name)
Response.Write(finish(m_ValueList.Item(Name)))
End Sub
End Class
%>
3、使用例子
下面举三个例子进行说明。
1)简单的值替换
模板文件为myTemple.tpl,内容:
<html><title>ASP模板简单替换</title><body>
祝贺!你赢了一辆{some_color}法拉利!
</body>
下面是ASP代码(kktTemplate.inc.asp就是上面给出的模板类):
<!--#INCLUDE VIRTUAL="kktTemplate.inc.asp"-->
<%
dim my_color,