Document directory
- Features
- Attribute
- Object
- Method
- Internal Variable
- Code
- Instance
Suitable for templates with few loops.
If internal loops are not implemented, regular expressions are required, which is a waste of resources and time. For details, refer to this article.
Features
Private or public cache can be set to improve efficiency
You can use stream or FSO components freely.
Supports custom file encoding.
Saved files
Attribute name
Text, the Template Name. It is mainly used to divide the public Cache Time Zone into different templates.
Format
Text, file encoding type, configurable value.
Object
Text, using components, you can set the value:
Stream
FSO
Publiccache
Boolean value: the public cache is used. When enabled, the template file is saved to the application object. Other objects that reference this template class can be read from the cache by setting the same name value and opening the public cache. (Load method)
Privatecache
Boolean value. If private cache is used, the template file is saved to the internal variables of the object when it is enabled. The same object that references this template class can be read. (Load method)
Direction
Text, template file directory, without the need to slash or backslash, such as: Template/Default
File
Text, template file name, front without a slash or backslash, such as default.html
Savedirection
Text, saving the directory where the file is located, without the need to slash or backslash, such as: HTML/Default
SaveFile
Text, save the file name, without a slash or backslash on the front side, such as default.html
Object Code
Text, the current text. This object is replaced by the setvar method. When the load method is used, the template is reloaded to this object.
Storage
Text, saved text. When the savefront or savelast method is used, the text in the Code object is saved to the beginning or end of the object. It can be used for loop to get all the code
Method pain
Clear public and private caches (forcibly reload templates from files)
Clearpubliccache
Clear public Cache
Clearprivatecache
Clear private Cache
Clearcode
Clear code object
Clearstorage
Clear storage objects
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
Replace the text in the current Code object
Parameter: the text to be replaced.
Load
Load the template file to the Code object. When the template file is enabled and there is a private cache, it is loaded from the private cache. When it is enabled and there is a public cache, it is loaded from the public cache. If there is no cache, it is loaded from the file.
Internal Variable ccstrpath
Default root directory
Ccstrcookiename
Default Application object name prefix
Code
Class ccclstemplate
Private ccstrcode, ccstrstorage
Private ccstrcachecode
Private ccblnpubliccache, ccblnprivatecache
Private ccstrname, ccstrcookiename
Private ccstrdirection, ccstrsavedirection, ccstrfile, ccstrsavefile, ccstrpath
Private ccobjstream, ccobjfso, ccstrformat, cceffecbject, 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
Ccatebject = 1' default template read/save 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
Cc1_bject = 2
Else
Cc1_bject = 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 upload ache
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 cc1_bject = 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
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 cc1_bject = 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
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
Instance
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 the template directory and file name
Objtemplate. Direction = "test"
Objtemplate. File = "test.html"
'Set the Directory and file name for saving the file
Objtemplate. savedirection = "test"
Objtemplate. SaveFile = "test3.html"
'Load Template
Call objtemplate. Load
'To replace text
Call objtemplate. setvar ("<# test #>", "Hello world .")
'Save the text to storage for temporary storage
Call objtemplate. savelast
'Reload the template and reload it from the private cache to improve efficiency
Call objtemplate. Load
'Replace with other values
Call objtemplate. setvar ("<# test #>", "by cloudream .")
'Save to the end of storage for temporary storage
Call objtemplate. savelast
'Save the code to the file
Call objtemplate. savecode
Response. Write objtemplate. Storage
Set objtemplate = nothing
Display result
Hello world. By cloudream.
Save file results
By cloudream.