Published: Wednesday, September 13, 2000
An ASP Template Class
By James Q. Stansfield
| An Update to this Class! |
|
Author James Stansfield has released a new version of the ASP Template Class, which you can read about
and download from: An ASP Template Class (version 1.5).
|
Introduction
Are your web pages ASP-spaghetti, a mess of ASP code and HTML? Are you tired of having to walk this code line by line each time you need to make basic changes to your HTML layout? (Or every time the web designer wants to change a font size, color or table size?) Cascading Style Sheets (CSS) have alleviated much of these problems they don't handle physical (multi-table based) layouts very well.
How would you (or your designer) like to edit a single HTML file to change the layout of the entire site without you having to change a single ASP code?
This article will explain how to use a VBscript class that utilizes template files for HTML presentation. (For more information on using classes with VBScript, be sure to read: Using Classes within VBScript!) The class is pretty basic but illustrates a very good method of separating HTML from ASP. In a second article I will expand upon the class to show it's true potential by creating a simple portal with multiple templates.
With most personal Web/hobby sites the work of design and development falls to the same person - you - and this technique may not really be needed for smaller sites but I know we all enjoy learning (read: playing with) ASP code as much as possible. However, some of us are currently employed as Web Developers and the design portion doesn't fall to us very often, if at all. (Huzzah!)
So we want to make it as easy as possible for the creative types to make whatever changes they feel are warranted. So let them use whichever HTML editor they want... After all, not everyone drools at the thought of creating websites from scratch at the command prompt! (Okay, maybe only I do.)
Background
First I would like to explain the path that led me to this templating technique. In previous sites I've used #INCLUDE statements for headers, footers, sidebars etc...for example:
<!--#INCLUDE FILE="HEAD.HTML"-->
<%
'Dynamic Content Goes Here...
%>
<!--#INCLUDE FILE="FOOT.HTML"-->
|
This works very well, however there are two problems.
Next, I moved away from include files and started loading and displaying the files myself with a custom written routine. This had the same 'fragmented file' drawback as using #INCLUDE files, but now I could handle missing files etc, using the load_File() subroutine:
Sub load_File(sFile)
Dim oFSO, oFILE
Set oFSO = createobject("Scripting.FileSystemObject")
If oFSO.FileExists(server.mappath(sFile)) then
Set oFILE = oFSO.OpenTextFile(server.mappath(sFile))
Do while not oFILE.AtEndOfStream
Response.Write(trim(oFILE.ReadLine) & VbCrLf)
Loop
oFILE.Close
set oFILE = nothing
Else
'Trap Missing File Error Here
End If
Set oFSO = nothing
End sub
|
The load_file() subroutine expects a single argument that contains the path & file name that you want displayed. The routine then checks for the existence of the file before opening and send line by line to the browser.
In Part 2 we'll look at a third alternative to keeping content and layout
separate - using templates!
Read Part 2!