使用模板实现ASP代码与页面分离(2)
2、模板类的实现
给出具体代码之前,先把主要函数列出,用过PHPLIB的朋友应该对此很熟悉了:
1)Public Sub set_root(ByVal Value) 设定模板默认目录
2)Public Sub set_file(ByVal handle,ByVal filename) 读取文件
3)Public Sub set_var(ByVal Name, ByVal Value, ByVal Append) 设置映射数据-替换变量
4)Public Sub unset_var(ByVal Name) 取消数据映射
5)Public Sub set_block(ByVal Parent, ByVal BlockTag, ByVal Name) 设置数据块
6)Public Sub set_unknowns(ByVal unknowns) 设定未指定映射的标记处理方式
7)Public Sub parse(ByVal Name, ByVal BlockTag, ByVal Append) 执行模板文件与数据的结合
8)Public Sub p(ByVal Name) 输出处理结果
实现代码:
<%
'=======================================================================
' CLASS NAME: kktTemplate ASP页面模板对象
' DESIGN BY : 彭国辉
' DATE: 2004-07-05
' WEBSITE: http://kacarton.yeah.net/
' EMAIL: kacarton@sohu.com
'
' 本对象中使用了set_var、set_block等命名方法是为了兼容phplib
'=======================================================================
Class kktTemplate
Private m_FileName, m_Root, m_Unknowns, m_LastError, m_HaltOnErr
Private m_ValueList, m_BlockList
Private m_RegExp
' 构造函数
Private Sub Class_Initialize
Set m_ValueList = CreateObject("Scripting.Dictionary")
Set m_BlockList = CreateObject("Scripting.Dictionary")
set m_RegExp = New RegExp
m_RegExp.IgnoreCase = True
m_RegExp.Global = True
m_FileName = ""
m_Root = ""
m_Unknowns = "remove"
m_LastError = ""
m_HaltOnErr = true
End Sub
' 析构函数
Private Sub Class_Terminate
Set m_RegExp = Nothing
Set m_BlockMatches = Nothing
Set m_ValueMatches = nothing
End Sub
Public Property Get ClassName()
ClassName = "kktTemplate"
End Property
Public Property Get Version()
Version = "1.0"
End Property
Public Sub About()
Response.Write("kktTemplate ASP页面模板类<br>" & vbCrLf &_
"程序设计:彭国辉 2004-07-05<br>" & vbCrLf &_
"个人网站:<a href='http://kacarton.yeah.net'>http://kacarton.yeah.net<;/a><br>" & vbCrLf &_
内容版权声明:除非注明,否则皆为本站原创文章。