Features can set private cache or public cache to improve efficiency. You can freely choose to use Stream component or FSO component. Support custom file encoding and save file
attributes.
Name
Text, the template name, mainly used to distinguish different templates when using the public cache.
Format
Text, file encoding type, settable value.
Object
Text, using components, can set values:
Stream
FSO
PublicCache
Boolean value, use the public cache. When turned on, the template file will be saved to the Application object. Other objects that reference this template class can read from the cache by setting the same Name value and also opening the public cache. (Load method)
PrivateCache
Boolean value, use private cache. When turned on, the template file will be saved to the internal variable of the object, and the same object that references this template class can read it. (Load method)
Direction
Text, directory where the template file is located, no slashes or backslashes before and after, such as: template/default
File
Text, template file name, no slash or backslash in front, such as: default.html
SaveDirection
Text, the directory where the file is saved, without slashes or backslashes before and after, such as: html/default
SaveFile
Text, save file name, no slash or backslash in front, such as: default.html
object
Code
Text, the current text, this object is replaced when using the SetVar method, and the template is overloaded to this object when using the Load method
Storage
Text, saved text. When using the SaveFront or SaveLast method, the text in the Code object is saved to the beginning or end of this object. It can be used to get all the codes after looping.
method
ClearCache
Clear public and private caches (force template reload from file)
ClearPublicCache
Clear public cache
ClearPrivateCache
Clear private cache
ClearCode
Clear Code object
ClearStorage
Clear Storage object
SaveFront
Save the text in the current Code object to the beginning of the Storage object
SaveLast
Save the text in the current Code object to the end of the Storage object
SaveCode
Save the text in the current Code object to a file
SaveStorage
Save the text in the current Storage object to a file
SetVar
Parameters for replacing text in the current Code object: text to be replaced, text to be replaced
Load
Load the template file into the Code object. When the private cache is enabled and exists, it is loaded from the private cache. When it is enabled and the public cache exists, it is loaded from the public cache. If there is no cache, it is loaded from the file.
internal variables
ccStrPath
Default root directory
ccStrCookieName
Default Application object name prefix
code
Class ccClsTemplate
Private ccStrCode,ccStrStorage
PrivateccStrCacheCode
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" 'Default name
ccBlnPublicCache = False
ccBlnPrivateCache = False
ccStrFile = "cache.html"
ccStrSaveFile = "save_cache.html"
ccStrCookieName = "ccClass_Template" 'Application object name prefix
ccStrFormat = "UTF-8" 'UTF-8|ASCII|GB2312|BIG5
ccIntFormat = -1
ccIntObject = 1 'Default read/save template component 1:ADODB.Stream 2:FSO
ccStrPath = Server.MapPath("./")&"" 'Default root path
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
Example
Template file content
<#test#>
ASP program code
Dim objTemplate
Set objTemplate = New ccClsTemplate
objTemplate.Name = "Test"
objTemplate.Format = "UTF-8"
'Enable caching
objTemplate.PublicCache = True
objTemplate.PrivateCache = True
'Set template directory and file name
objTemplate.Direction = "test"
objTemplate.File = "test.html"
'Set the save file directory and file name
objTemplate.SaveDirection = "test"
objTemplate.SaveFile = "test3.html"
'Load template
Call objTemplate.Load
'Perform text replacement
Call objTemplate.SetVar("<#test#>","Hello world.")
'Save the text to Storage
Call objTemplate.SaveLast
'Reload the template, which will be reloaded from the private cache to improve efficiency.
Call objTemplate.Load
'replace with other value
Call objTemplate.SetVar("<#test#>"," By Cloudream.")
'Save to the end of Storage for temporary storage
Call objTemplate.SaveLast
'Save Code to file
Call objTemplate.SaveCode
Response.Write objTemplate.Storage
Set objTemplate = Nothing
Show results
Hello world. By Cloudream.
Save file results
By Cloudream.