Code Separation index.asp
-----------------------------------------------------------------------
<!--#include file= "templateclass.asp"-->
<%
' This is the code used to load and display the template.
' How to clean it '!!! :)
Sub Go ()
Dim otemplate
Set otemplate = new template
With Otemplate
. Usetemplate ("Message.tpl")
. Tag ("date") = Date ()
. Tag ("self") = "<div style= ' background: #dddddd ' >" &. GetTemplate ("Message.tpl", True) & "</div>"
. Tag ("aspcode") =. GetTemplate ("index.asp", false)
. Tag ("howtouse") =. GetTemplate ("Howtouse.tpl", false)
. Display ()
End With
Set otemplate = Nothing
End Sub
Go ()
%>
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, 1999
Class template
' This variable stores the template
Private 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 String
Dim oFSO ' As Object
Dim Otemplate ' As Object
Dim Temptemplate ' As String
' Open type for the template (read-only)
Const ForReading = 1,boolcreatefile = False
If IsNull (encodetohtml) or encodetohtml = "" or encodetohtml = False Then
Encodetohtml = False
Else
Encodetohtml = True
End If
On Error Resume Next
' Create FileSystemObject
Set oFSO = Server.CreateObject ("Scripting.FileSystemObject")
' Create Template Object
Set otemplate = Ofso.opentextfile (Server.MapPath (pathfilename), forreading,boolcreatefile)
If Err <> 0 Then
Err.Clear
Exit function
End If
' Get the whole file as a string
Temptemplate = Otemplate.readall
' Encode template to HTML?
If Encodetohtml Then
GetTemplate = tohtml (temptemplate)
Else
GetTemplate = Temptemplate
End If
' Close the template
Otemplate.close
' Free ' of the server resources
Set otemplate = Nothing
Set oFSO = Nothing
End Function
' This procedure gets and stores a template
Public Sub Usetemplate (pathfilename)
Thistemplate = GetTemplate (pathfilename,false)
End Sub
' This property replaces tags with the user ' s template
Public Property Let tag (tagname,userstring)
Dim ld, RD ' As String
Dim Temptag ' As String
Dim Tagstart, Tagend ' As Integer
LD = "<!--"
RD = "-->"
Tagstart = 1
Do While Tagstart > 0 and Tagstart < Len (thistemplate)
Tagstart = InStr (TAGSTART,THISTEMPLATE,LD)
If Tagstart > 0 Then
Tagend = InStr (TAGSTART,THISTEMPLATE,RD)
If Tagend > (Tagstart + 3) Then
Temptag = Mid (Thistemplate,tagstart + 4,tagend-(tagstart+4))
if (Trim (temptag) = tagname) or (Temptag = tagname) Then
If IsNull (userstring) Then
Thistemplate = replace (thistemplate,ld & temptag & Rd, "")
Else
Thistemplate = replace (Thistemplate,ld & Temptag & rd,userstring)
End If
Exit Do
Else
Tagstart = Tagstart + 4
End If
End If
End If
Loop
End Property
Public Sub Removetags ()
Dim ld, RD ' As String
Dim Temptag ' As String
Dim Tagstart, Tagend ' As Integer
LD = "<!--"
RD = "-->"
Tagstart = 1
Do While Tagstart > 0 and Tagstart < Len (thistemplate)
Tagstart = InStr (TAGSTART,THISTEMPLATE,LD)
If Tagstart > 0 Then
Tagend = InStr (TAGSTART,THISTEMPLATE,RD)
If Tagend > (Tagstart + 3) Then
Temptag = Mid (Thistemplate,tagstart + 4,tagend-(tagstart+4))
Thistemplate = replace (thistemplate,ld & temptag & Rd, "")
Tagstart = Tagend
End If
End If
Loop
End Sub
' This property allows the "user to assign" current template
Public Property Let Thistemplate (template_as_string)
If VarType (template_as_string) = Vbstring _
or VarType (template_as_string) = Vbnull Then
MyTemplate = template_as_string
End If
End Property