特性
可設定私有緩存或公共緩存,提高效率
可自由選擇使用 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.
保存文件結果