Code Separation method

Source: Internet
Author: User
Tags exit integer tag name tagname
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


' This property returns the current template
Public Property Get Thistemplate () ' As String
Thistemplate = MyTemplate
End Property


' This subroutine displays the current template
Public Sub Display ()
Response.Write Thistemplate
End Sub

' This subroutine encodes the current template to HTML
Public Sub HTMLEncode ()
Tohtml (Thistemplate)
End Sub


' This procedure saves the current template to the server
Public Sub SaveAs (Pathfilename)
Dim oFSO ' As Object
Dim Otemplate,obackup ' As Object
Dim Strtruepath ' As String

' Open type for the template (read-only)
Const ForReading = 1,forwriting = 2,boolcreatefile = True

On Error Resume Next

' Create FileSystemObject
Set oFSO = Server.CreateObject ("Scripting.FileSystemObject")

' Create Template Object
Strtruepath = Server.MapPath (pathfilename)
If Ofso.fileexists (Strtruepath) Then
Ofso.copyfile Strtruepath, Strtruepath & ". Bak", True
End If
Set otemplate = Ofso.opentextfile (strtruepath,forwriting,boolcreatefile)
If Err <> 0 Then
Err.Clear
Exit Sub
End If

' Write the whole template to the server
Otemplate.write thistemplate

' Close the template
Otemplate.close

' Free ' of the server resources
Set otemplate = Nothing

Set oFSO = Nothing
End Sub


' This function encodes the text to HTML
Private Function tohtml (temptemplate)
Temptemplate = replace (temptemplate, "<", "<")
Temptemplate = replace (Temptemplate, "", "")
Temptemplate = replace (Temptemplate,vbcrlf, "<br/>")

tohtml = Temptemplate
End Function

' This procedure clears the variable of the current template
' is stored in ', the object is created.
Private Sub Class_Initialize ()
MyTemplate = ""
End Sub


' This procedure clears the variable of the current template
' is stored in ', the object is terminated.
Private Sub Class_Terminate ()
Set MyTemplate = Nothing
End Sub
End Class
%>

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

<div style= "background: #dddddd" ><p><b>thistemplate</b> = <i>String</i>
Loads a dynamically built template into the template object. Use this method
When there isn ' t a file to read from.</p></div>


<div style= "background: #dddddd" ><p><b>tag (<i>tag name as string</i>) </b> = <i >String</i>
Replaces all occurrences of a tagged within the current template with the specified string.</p>

<p><i>variable</i> = <b>gettemplate (<i>path as String</i>,<i>[encode to HTML] as boolean</i> </b>
Returns the template from the specified path; However, it isn ' t loaded into the object.</p></div>

<div style= "background: #dddddd" ><p><b>usetemplate (<i>path as string</i>) </b>
Loads the template from the specified path into the object.</p>

<p><b>htmlencode () </b>
Encodes the current template loaded into html.</p>

<p><b>display () </b>
Writes the current template to the user ' s browser.</p>

<p><b>removetags () </b>
Removes all tags that haven ' t been replaced.</p>

<p><b>saveas (<i>path as html</i>) </b>
Saves the current template to the specified path. Use the
Want to save the changes made to the template.
Note:if a file of the same name exists at the specified path, ". Bak" would be
Added to the "end of" of its name; Thus "MYFILE.TPL" would become "Myfile.tpl.bak" .</p></div>
</div>

Message.tpl
------------------------------------------------------------------------
<body>
<pre>
This is a example of how do I template class.
Dynamic data for your template can come from anywhere.

It can come from a built-in ASP function: <!--date-->

It can come from a file:
<!--self-->

Can even use it as a online source editing tool for your ASP code:
<form><textarea cols= "rows=" ><!--aspcode--></textarea></form>


<!--howtouse-->
Note:templates can ' t have any ASP code in them. It ' ll only process HTML.
This is good practice, because it produces cleaner code and HTML.
</pre>
</body>

I hope I can help you!




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.