当前位置: 网学 > 编程文档 > ASP > 正文

使用模板实现ASP代码与页面分离

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/17
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 &_
"电子邮件:<a href=''mailto:kacarton@sohu.com''>kacarton@sohu.com</a><br>")
End Sub

''检查目录是否存在
Public Function FolderExist(ByVal path)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
FolderExist = fso.FolderExists(Server.MapPath(path))
Set fso = Nothing
End Function
''读取文件内容
Private Function LoadFile()
Dim Filename, fso, hndFile
Filename = m_Root
If Right(Filename, 1)<>"/" And Right(Filename, 1)<>"\" Then Filename = Filename & "/"
Filename = Server.MapPath(Filename & m_FileName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(Filename) Then ShowError("模板文件" & m_FileName & "不存在!")
set hndFile = fso.OpenTextFile(Filename)
LoadFile = hndFile.ReadAll
Set hndFile = Nothing
Set fso = Nothing
If LoadFile = "" Then ShowError("不能读取模板文件" & m_FileName & "或文件为空!")
End Function

''处理错误信息
Private Sub ShowError(ByVal msg)
m_LastError = msg
Response.Write "<font color=red style=''font-size;14px''><b>模板错误:" & msg & "</b></font><br>"
If m_HaltOnErr Then Response.End
End Sub

''设置模板文件默认目录
''Ex: kktTemplate.set_root("/tmplate")
'' kktTemplate.Root = "/tmplate"
'' root = kktTemplate.get_root()
'' root = kktTemplate.Root
''使用类似set_root这样的命名方法是为了兼容phplib,以下将不再重复说明
Public Sub set_root(ByVal Value)
If Not FolderExist(Value) Then ShowError(Value & "不是有效目录或目录不存在!")
m_Root = Value
End Sub
Public Function get_root()
get_root = m_Root
End Function
Public Property Let Root(ByVal Value)
set_root(Value)
End Property
Public Property Get Root()
Root = m_Root
End Property

''设置模板文件
''Ex: kktTemplate.set_file("hndTpl", "index.htm")
''本类不支持多模板文件,handle为兼容phplib而保留
Public Sub set_file(ByVal handle,ByVal filename)
m_FileName = filename
m_BlockList.Add Handle, LoadFile()
End Sub
  • 上一篇资讯: 禁止站外提交表单
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号