ASP Common template class

Source: Internet
Author: User
Tags format exit save file trim root directory
Template

ASP generic template classes.

Suitable for templates with fewer loops. Internal loops are not implemented, and regular expressions are used, which is a waste of resources and time, if you need to use this article.

Characteristics
can set private cache or public cache to improve efficiency
Free to choose to use the Stream component or the FSO component
Support for custom file encoding
To save a file

Property

Name
Text, the template name, used primarily to differentiate different templates when using common caching.

Format
Text, file encoding type, can set value.

Object
Text, using components, you can set values:

Stream
Fso

Publiccache
Boolean value, using the public cache, the template file is saved to the Application object when it is opened, and other objects referencing the template class set the same name value and also open the common cache to read from the cache. (Load method)

Privatecache
Boolean value, using private caching, the template file will be saved to an object internal variable when opened, and the same object referencing this template class can be read. (Load method)

Direction
Text, the directory where the template file is located, without either a slash or a backslash, such as: Template/default

File
text, template filename, without a slash or backslash, such as: default.html

Savedirection
Text, save the file in the directory, no forward or backward slash or backslash, such as: Html/default

SaveFile
Text, save file name, no trailing slash or backslash, such as: default.html

Object

Code
Text, current text, replacing this object when using the Setvar method, overloading the template to this object when using the Load method

Storage
Text, saved text, saving text in a code object to the beginning or end of this object when using the Savefront or Savelast method, and can be used to get all the code after the loop

Method

ClearCache
Clear public and private caches (force templates to be overloaded from files)

Clearpubliccache
Clear public cache

Clearprivatecache
Clear Private Cache

Clearcode
Clear the Code object

Clearstorage
Clear Storage Object

Savefront
Saves the text in the current code object to the beginning of the storage object

Savelast
Saves the text in the current code object to the end of the storage object

Savecode
Saves text in the current code object to a file

Savestorage
Saves text in the current storage object to a file

SetVar
Replaces text in the current code object
Argument: text that needs to be replaced, text to replace

Load
Load the template file into the code object, load from the private cache when the private cache is turned on and on, and when it is on the public cache, load from the public cache, and load from the file without caching

Internal variables

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,ccintobject,ccobjtext, Ccintformat

Private Sub Class_Initialize
Ccstrname = "Default" defaults to 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 (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

Instance

Template file Contents

< #test #>

ASP program code

Dim objtemplate
Set objtemplate = New ccclstemplate
Objtemplate.name = "Test"
Objtemplate.format = "UTF-8"
' Open cache
Objtemplate.publiccache = True
Objtemplate.privatecache = True
' Set the template directory and file name
objtemplate.direction = "Test"
Objtemplate.file = "Test.html"
' Set save file directory and filename
objtemplate.savedirection = "Test"
Objtemplate.savefile = "Test3.html"
' Load template
Call Objtemplate.load
' For text substitution
Call Objtemplate.setvar ("< #test #>", "Hello world.")
' Save text to storage temporary
Call Objtemplate.savelast
' Reload the template, which will reload from the private cache to increase efficiency
Call Objtemplate.load
' Replace with a different value
Call Objtemplate.setvar ("< #test #>", "by Cloudream.")
' Save to storage end of 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.