An ASP Template ClassBy 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).|
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.)
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:
This works very well, however there are two problems.
1. Putting the dynamic content aside, the designer can't see what the page looks like in its entirety because the HTML portion is split into fragments. So in these cases the designer makes one file and it was up to me to split the HTML into the pieces that I needed.
2. If for some reason an included file is missing you get untrapable runtime errors.
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 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!