ASP通用模板类

80酷酷网    80kuku.com

  

特性
可设定私有缓存或公共缓存,提高效率
可自由选择使用 Stream 组件或 FSO 组件
支持自定义文件编码
可保存文件

属性


Name
文本,该模板名称,主要用于使用公共缓存时区分不同模板。


Format
文本,文件编码类型,可设置值。


Object
文本,使用组件,可设置值:


Stream
FSO

PublicCache
布尔值,使用公共缓存,开启时模板文件将保存到Application对象,其他引用此模板类的对象设置相同Name值并同样打开公共缓存即可从缓存读取。(Load方法)


PrivateCache
布尔值,使用私有缓存,开启时模板文件将保存到对象内部变量,同一引用此模板类的对象可读取。(Load方法)


Direction
文本,模板文件所在目录,前后无需斜杠或反斜杠,如:template/default


File
文本,模板文件名,前边无需斜杠或反斜杠,如:default.html


SaveDirection
文本,保存文件所在目录,前后无需斜杠或反斜杠,如:html/default


SaveFile
文本,保存文件名,前边无需斜杠或反斜杠,如:default.html

对象


Code
文本,当前文本,使用SetVar方法时对此对象进行替换,使用Load方法时将模板重载到此对象


Storage
文本,已保存文本,使用SaveFront或SaveLast方法时将Code对象中文本保存到此对象的开头或结尾,可用于循环后得到所有代码

方法


ClearCache
清除公共缓存和私有缓存(强制从文件重载模板)


ClearPublicCache
清除公共缓存


ClearPrivateCache
清除私有缓存


ClearCode
清除Code对象


ClearStorage
清除Storage对象


SaveFront
将当前Code对象中文本保存到Storage对象开头


SaveLast
将当前Code对象中文本保存到Storage对象结尾


SaveCode
将当前Code对象中文本保存到文件


SaveStorage
将当前Storage对象中文本保存到文件


SetVar
对当前Code对象中文本进行替换
参数:需要被替换的文本,欲替换后的文本


Load
将模板文件载入Code对象,当开启并存在私有缓存时,从私有缓存载入,当开启并存在公共缓存时,从公共缓存载入,若无缓存则从文件载入

内部变量


ccStrPath
默认根目录


ccStrCookieName
默认Application对象名前缀

代码


Class ccClsTemplate

  Private ccStrCode,ccStrStorage
  Private ccStrCacheCode
  Private ccBlnPublicCache,ccBlnPrivateCache
  Private ccStrName,ccStrCookieName
  Private ccStrDirection,ccStrSaveDirection,ccStrFile,ccStrSaveFile,ccStrPath
  Private ccObjStream,ccObjFSO,ccStrFormat,ccIntObject,ccObjText,ccIntFormat

  Private Sub Class_Initialize
    ccStrName = "default"    '默认名称
    ccBlnPublicCache = False
    ccBlnPrivateCache = False
    ccStrFile = "cache.html"
    ccStrSaveFile = "save_cache.html"
    ccStrCookieName = "ccClass_Template"  'Application对象名前缀
    ccStrFormat = "UTF-8"    'UTF-8|ASCII|GB2312|BIG5
    ccIntFormat = -1
    ccIntObject = 1        '默认读取/保存模板组件 1:ADODB.Stream 2:FSO
    ccStrPath = Server.MapPath("./")&"\"  '默认根路径
  End Sub

  Public Property Let Name(ccStrName_in)
    ccStrName = LCase(Trim(ccStrName_in))
  End Property

  Public Property Let Format(ccStrFormat_in)
    ccStrFormat = ccStrFormat_in
    If InStr(LCase(Trim(ccStrFormat_in)),"utf") > 0 Then
      ccIntFormat = -1
    Else
      ccIntFormat = 0
    End If
  End Property

  Public Property Let Object(ccStrObject_in)
    ccStrObject_in = LCase(Trim(ccStrObject_in))
    If InStr(ccStrObject_in,"fso") > 0 Then
      ccIntObject = 2
    Else
      ccIntObject = 1
    End If
  End Property

  Public Property Let PublicCache(ccBlnPublicCache_in)
    If ccBlnPublicCache_in = True Then
      ccBlnPublicCache = True
    Else
      ccBlnPublicCache = False
    End If
  End Property

  Public Property Let PrivateCache(ccBlnPrivateCache_in)
    If ccBlnPrivateCache_in = True Then
      ccBlnPrivateCache = True
    Else
      ccBlnPrivateCache = False
    End If
  End Property

  Public Property Let Direction(ccStrDirection_in)
    ccStrDirection = ccStrDirection_in
  End Property

  Public Property Let File(ccStrFile_in)
    If ccStrFile_in <> "" Then
      ccStrFile = ccStrFile_in
    End If
  End Property

  Public Property Let SaveDirection(ccStrSaveDirection_in)
    ccStrSaveDirection = ccStrSaveDirection_in
  End Property

  Public Property Let SaveFile(ccStrSaveFile_in)
    If ccStrSaveFile_in <> "" Then
      ccStrSaveFile = ccStrSaveFile_in
    End If
  End Property

  Public Property Get Code
    Code = ccStrCode
  End Property

  Public Property Get Storage
    Storage = ccStrStorage
  End Property

  Public Sub ClearCache
    Call ClearPrivateCache
    Call ClearPublicCache
  End Sub

  Public Sub ClearPrivateCache
    ccStrCacheCode = ""
  End Sub

  Public Sub ClearPublicCache
    Application(ccStrCookieName&ccStrName) = ""
  End Sub

  Public Sub ClearStorage
    ccStrStorage = ""
  End Sub

  Public Sub ClearCode
    ccStrCode = ""
  End Sub

  Public Sub SaveFront
    ccStrStorage = ccStrCode & ccStrStorage
  End Sub

  Public Sub SaveLast
    ccStrStorage = ccStrStorage & ccStrCode
  End Sub

  Public Sub SaveCode
    Call SaveToFile(1)
  End Sub

  Public Sub SaveStorage
    Call SaveToFile(2)
  End Sub

  Public Sub SetVar(ccStrTag_in,ccStrValue_in)
    ccStrCode = RePlace(ccStrCode,ccStrTag_in,ccStrValue_in)
  End Sub

  Private Sub SaveToFile(ccIntCode_in)
    Dim ccStrSaveCode
    If ccIntCode_in = 1 Then
      ccStrSaveCode = ccStrCode
    Else
      ccStrSaveCode = ccStrStorage
    End If
    If ccIntObject = 1 Then
      Set ccObjStream = Server.CreateObject("ADODB.Stream")
      With ccObjStream
        .Type = 2
        .Mode = 3
        .Open
        .Charset = ccStrFormat
        .Position = ccObjStream.Size
        .WriteText ccStrSaveCode
        .SaveToFile ccStrPath & ccStrSaveDirection & "\" & ccStrSaveFile,2
        .Close
      End With
      Set ccObjStream = Nothing
    Else
      Set ccObjFSO = CreateObject("Scripting.FileSystemObject")
      If ccObjFSO.FileExists(ccStrPath & ccStrSaveDirection & "\" & ccStrSaveFile) = True Then
        ccObjFSO.DeleteFile(ccStrPath & ccStrSaveDirection & "\" & ccStrSaveFile)
      End If
      Set ccObjText = ccObjFSO.OpenTextFile(ccStrPath & ccStrSaveDirection & "\" & ccStrSaveFile,2,True,ccIntFormat)
      ccObjText.Write ccStrSaveCode
      Set ccObjText = Nothing
      Set ccObjFSO = Nothing
    End If
    ccStrSaveCode = ""
  End Sub

  Public Sub Load
    ccStrCode = ""
    If ccBlnPrivateCache = True Then
      If ccFncIsEmpty(ccStrCacheCode) = False Then
        ccStrCode = ccStrCacheCode
        Exit Sub
      End If
    End If
    If ccBlnPublicCache = True Then
      If ccFncIsEmpty(Application(ccStrCookieName&ccStrName)) = False Then
        ccStrCode = Application(ccStrCookieName&ccStrName)
        Exit Sub
      End If
    End If
    If ccIntObject = 1 Then
      Set ccObjStream = Server.CreateObject("ADODB.Stream")
      With ccObjStream
        .Type = 2
        .Mode = 3
        .Open
        .Charset = ccStrFormat
        .Position = ccObjStream.Size
        .LoadFromFile ccStrPath & ccStrDirection & "\" & ccStrFile
        ccStrCode = .ReadText
        .Close
      End With
      Set ccObjStream = Nothing
    Else
      Set ccObjFSO = CreateObject("Scripting.FileSystemObject")
      If ccObjFSO.FileExists(ccStrPath & ccStrDirection & "\" & ccStrFile) = True Then
        Set ccObjText = ccObjFSO.OpenTextFile(ccStrPath & ccStrDirection & "\" & ccStrFile,1,False,ccIntFormat)
        ccStrCode = ccObjText.ReadAll
        Set ccObjText = Nothing
      End If
      Set ccObjFSO = Nothing
    End If
    If ccBlnPrivateCache = True Then
      ccStrCacheCode = ccStrCode
    End If
    If ccBlnPublicCache = True Then
      Application(ccStrCookieName&ccStrName) = ccStrCode
    End If
End Sub

End Class

Function ccFncIsEmpty(ByRef ccStrValue_in)
  If IsNull(ccStrValue_in) Or IsEmpty(ccStrValue_in) Or ccStrValue_in = "" Then
    ccFncIsEmpty = True
  Else
    ccFncIsEmpty = False
  End If
End Function


实例


模板文件内容

<#test#>

ASP程序代码


Dim objTemplate
Set objTemplate = New ccClsTemplate
objTemplate.Name = "Test"
objTemplate.Format = "UTF-8"
'开启缓存
objTemplate.PublicCache = True
objTemplate.PrivateCache = True
'设置模板目录和文件名
objTemplate.Direction = "test"
objTemplate.File = "test.html"
'设置保存文件目录和文件名
objTemplate.SaveDirection = "test"
objTemplate.SaveFile = "test3.html"
'载入模板
Call objTemplate.Load
'进行文本替换
Call objTemplate.SetVar("<#test#>","Hello world.")
'将文本保存至Storage暂存
Call objTemplate.SaveLast
'重新载入模板,此时将从私有缓存重新装载,提高效率
Call objTemplate.Load
'替换为其他值
Call objTemplate.SetVar("<#test#>"," By Cloudream.")
'保存至Storage结尾暂存
Call objTemplate.SaveLast
'保存Code至文件
Call objTemplate.SaveCode
Response.Write objTemplate.Storage

Set objTemplate = Nothing


显示结果


Hello world. By Cloudream.

保存文件结果


By Cloudream.

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: