External code separation method

xiaoxiao2021-03-06  90

Index.asp ------------------------------------------------- ------------------------- <% 'this is the code used to load And Display The Template. 'See How Clean IT IS !!! :) Sub Go () Dim Otemplate

Set Otemplate = New Template

With teemplate.useTemplate ("message.tpl"). Tag ("Date") = Date (). Tag ("Self") = "

" & .getTemplate ("Message. TPL ", TRUE) &" ". Tag (" aspcode ") = .getTemplate (" INDEX.ASP ", false) .tag (" howtouse ") = .getTemplate (" Howtouse.tpl ", false) .display () end with

Set Otemplate = Nothingend Subgo ()%>

TemplateClass.asp ----------------------------------------------- ------------------------- <% 'this is the template Object. It allows the creation, reading' and editing of templates on the server. ' Created by: Christopher Brown-Floyd 'Date: November 3, 1999Class Template

'This variable stores the templateprivate mytemplate' AS STRING

'This method gets a template from the server and returns' the whole file as a string.public function gettemplate (pathfilename, encodetohtml) 'as stringdim oFSO' as objectdim oTemplate 'as objectdim temptemplate' as string

'Open type for the template (read-only) const forreading = 1, boolcreatefile = false

If isnull (encodetohtml) or encodetohtml = "" or encodetohtml = false burodetohtml = falseelseencodetohtml = trueEnd IF

On Error ResMject 'Create FileSystemObjectSet OFSO = Server.createObject ("scripting.filesystemObject")

'Create template objectset oTemplate = oFSO.opentextfile (server.mappath (pathfilename), forreading, boolcreatefile) if err <> 0 thenerr.clearexit functionend if' Get the whole file as a stringtemptemplate = oTemplate.readall 'Encode template to HTML? If Encodetohtml ThengetTemplate = Tohtml (TempTemplate) ElsegetTemplate = TempTemplateEnd IF

'Close the TemplateTemplate.Close

'Free the server resourcesset teemplate = Nothing

Set ofso = nothingend function

'This Procedure Gets and Stores A TemplatePublic Sub UseTemplate (PathFileName) thistemplate = getTemplate (PathfileName, False) End Sub

'This Property Replaces Tags with the user's templatepublic Property Let Tag (Tagname, UserString) Dim LD, RD' AS Stringdim Temptag 'AS Stringdim TagStart, TAGEND' AS INTEGER

LD = "" tagstart = 1

do while tagstart> 0 and tagstart 0 thentagend = instr (tagstart, thistemplate, rd) if tagend> (tagstart 3) thentemptag = mid (thistemplate, tagstart 4, tagend- (tagstart 4)) if (trim (temptag) = tagname) or (temptag = tagname) thenif IsNull (userstring) thenthistemplate = replace (thistemplate, ld & temptag & rd, "") elsethistemplate = replace (Thistemplate, LD & Temptag & Rd, UserString) end ifxit doelsetagStart = TagStart 4END IFEND IFEND IFELOOPEND Property

Public Sub Removetags () Dim LD, RD 'As Stringdim Temptag' AS Stringdim TagStart, TAGEND 'AS INTEGER

LD = "" tagstart = 1

do while tagstart> 0 and tagstart 0 thentagend = instr (tagstart, thistemplate, rd) if tagend> (tagstart 3) thentemptag = mid (thistemplate, tagstart 4, tagend- (tagstart 4)) thistemplate = replace (thistemplate, ld & temptag & rd, "") tagstart = tagendend ifend ifloopend sub 'This property allows the user to assign the current templatepublic property let thistemplate (template_as_string) if Vartype (Template_as_String) = VBString_or Vartype = VBnull ThenmyTemplate = Template_as_stringend iFend Property

'This Property Returns The Current TemplatePublic Property Get thistemplate ()' as stringthistemplate = MyTemplateEnd Property

'This subroutine displays the current templatepublic sub displet () Response.write thistemplateend Sub

'This subroutine encodes the current template to htmlpublic sub htmlencode () TOHTML (thistemplate) End Sub

'This Procedure Saves The Current Template To The Serverpublic Sub Saves (PathfileName) DIM OFSO' As Objectdim Otemplate, OBACKUP 'As Objectdim StRTRUEPATH' AS STRING

'Open type for the template (read-only) const forreading = 1, forwriting = 2, boolcreatefile = true

ON Error ResMe next

'Create FileSystemObjectSet OFSO = Server.createObject ("Scripting.FileSystemObject")

'Create template objectstrTruePath = server.mappath (pathfilename) if oFSO.fileexists (strTruePath) thenoFSO.copyfile strTruePath, strTruePath & ".bak", trueend ifset oTemplate = oFSO.opentextfile (strTruePath, forwriting, boolcreatefile) if err <> 0 thenerr .cleArExit Subend if 'Write The Whole Template to the ServeroteMplate.write thistemplate

'Close the TemplateTemplate.Close

'Free the server resourcesset teemplate = Nothing

Set ofso = nothingend sub

'This function encodes text to htmlprivate function tohtml (temptemplate) temptemplate = replace (temptemplate, "<", "<") temptemplate = replace (temptemplate, "", "") temptemplate = replace (temptemplate, vbcrlf, "
")

TOHTML = TempTemplateEnd Function

'This procedure clears the variable what the current template' is stored in when the object is created.private sub coplass_initialize () mytemplate = "" End Sub

'This procedure clears the variable what the current template' is stored in When a object is terminated.private sub class_terminate () set mytemplate = nothingnd suend class%>

Howtouse.tpl ------------------------------------------------- -------------------------

Object's Properties:

thistemplate = string loads a dynamically built template infa Template Object . Use this methodwhen there isn't a file to read from.

Object's procedures:

tag ( tag name as string ) = string Replaces All Occurrence of a Tag with the specified string.

variable = getTemplate ( Path as string , [Encode to HTML] As Boolean ) returns the template from the specified path; however, it isn't loading into the object.

Object's methods:

useemplate ( path as string ) loads the template from the specified Path Into the Object.

htmlencode () EncoDes the current template loaded Into HTML.

display () Writes the current template to the user's brohing.

removetags () removes all tags what have't been replaced.

saveas () saves the current template to the specified path. Use this method when Youwant to save the change: if a Template.note: if A FILE OF THE SAME NAME EXISTS At the Specified Path, ".bak" Will Beadded to the end of its name; thus "myfile.tpl" would be "myfile.tpl.bak".

Message.tpl --------------------------------------------- -------------------------

 this is an example of how to use my template class.dynamic data for your Template can come from anywhere.

IT can come from a built-in asp function: it can come from a file:

You can Even Use IT As an Online Source Editing Tool For Your ASP Code: