To read the article online, visit

Enhancing Web Site Maintainability using Templates

By Christiaan Verwijs

Before I start: This article describes a website-building method you can use. The scripts here are not copy-paste scripts, but show you how to use the method.

Most people use ASP primarily to mould data taken from a database into a pre-written template. That is, they create a Web page to output the contents in a particular database table. This allows far greater flexibility and maintainability than hard coding values into a Web page because the data in the database can be changed using an online-form.

Now, there's one big limitation to this method - the template used in the Web page to output and format the database record is not dynamic. Once it's online, it's rather difficult to change it. Besides, editing the template can only be done by people with knowledge of ASP, and when your site has a rather big staff, you can be sure that most of them do not know how to use it - resulting in you having to do it.

I came across this problem when building a site with a staff of over 10 people. The only person able to use ASP was me. However, I wanted to allow everybody to change the templates - and preferably through an online-form.

Another reason for this 'project' was that I wanted to be able to truly reuse a template on a different page without having to copy the HTML/ASP-code to that page. I wanted to be able to use the exact same template for every page - this allowed me to change a whole bunch of pages by changing one of the templates used on the pages.

I had several choices, I could use the FileSystemObject to read the file into a form-field, or to store the entire template in the database. Basically, you can go for both I think, but I chose for the database-approach. (For more information on how to employ a method using the FileSystemObject, check out Ryan S.'s, Using Template Files to Create Dynamic ASP Pages.)

Now, some of you may wonder how I got this to work? After all, it's not possible to store ASP-scripts in a database. Indeed, ASP wasn't a choice here, I had to come up with a way to store the template in the database but maintain the dynamic nature.

Before I start throwing scripts at you, let me explain the basics of my approach:

Basically, a template is nothing more than a piece of HTML-code with special 'slots' where data can be dynamically entered. Usually, you would use ASP-code to build these slots. But in my case, since use of ASP wasn't possible here, I chose for another type of 'slots'.

Take a look at this example

Set rst = CreateObject("ADODB.recordset")
FindSQL = "SELECT name, address FROM persons" FindSQL, ObjConn, adOpenForwardOnly

<FONT FACE="verdana" SIZE="1">
<B><%=rst.fields("name")%></B> - <%=rst.fields("address")%>

This is a normal ASP-template. The template can display both the name and the address of a person or whatever you want. I chose to change the slots, turning the template into this:

<FONT FACE="verdana" SIZE="1"><B>[1]</B> - [2]<FONT>

A template like this can be stored in a database. It doesn't contain any ASP-code. But how did I get the server to insert the (dynamic) data into the slots? Well, this may be a bit technical, so hang in there.

I used GetRows method of the Recordset object to place the entire recordset into an array. (See the technical documentation for GetRows.) First of all, this is the only way to get this template to work because it's far easier to make a slot refer to a number (representing the array-row) and second of all - it's faster.

Set rst = CreateObject("ADODB.recordset")
FindSQL = "SELECT name, address FROM persons" FindSQL, ObjConn, adOpenForwardOnly

Recordarray = rst.getrows
MaxRecords = Ubound(Recordarray, 2) + 1

'Start cycling through the recordarray, reading every record
FOR MinRecords = 0 TO MaxRecords

   'Template to use for each record read
   Template = "<FONT FACE='verdana' SIZE='1'><B>[1]</B> - [2]<FONT>"

   'Number of slots in template 
   SlotCounter = 2

   'Read data from recordarray and replace correct slot with data
   FOR CurrentSlot = 0 TO SlotCounter - 1
      Current = Recordarray(CurrentSlot, MinRecords)
      Template = Replace(Template, "[" & CurrentSlot + 1 & "]",Current)         
   Response.write Template & "<BR>"
   Minrecords = Minrecords + 1

The output of this script can be:

O. Jackson - Wisconsin, USA
C. Wagenmaker - Amsterdam, NL
O. Heinrich - Berlin, GER

This script is just a small example. To keep things easy, I defined the template-to-use within the function (the string Template) - whichs isn't very smart because we want it to be in the database. So, if you wish to use this approach you should place the template in a table called 'templates' and retrieve the template everytime you wish to use it. Another thing you should change is Slotcounter. In my example, I defined how many slots existed within the template. It shouldn't be very difficult to make a function to count the number of slots so you don't have to worry about that yourself.

Using this method can greatly enhance maintainability, but there are some things you should take into consideration:

  • First of all, this method is not as fast as a normal template. I have never timed the difference, but I'm sure there is one, although not that big. If you know how to build your own ASP-component, I'd recommend you to place it in one to increase speed.
  • Second, this method does limit what you can do with the data. Since you cannot use ASP-code within the template, you must make sure that the data you wish to place in it is already correctly formatted.

There are some other things you will propably run into when using this method, but most of them can be solved pretty well I think. It just depends upon how complex you want to make the function building the templates.

On my site (which is not yet online), I've placed the templates in a database, as well as the data used to build the real HTML-code using these templates. In addition, I made a variety of templates for each page, so that visitors can chose the template they like best. And most of all, the staff can now edit the templates without requiring my assistance.

I, myself, have made a quite advanced version of this method. My engine (as I like to call it), can also cache templates by storing a pre-compiled version of the template + data in a special table. To increase speed, the engine can use a cached version instead of having to recompile the whole template and the data again. In addition, my engine also supports various advanced ASP-like functions for use within the template! For example: I can determine the number of characters to display, and how to format the data. But that's for the future ....

Happy programming!!

Questions, comments, suggestions? E-mail me at

Christiaan Verwijs is a 19-year old Business Information-Technologies student from the Netherlands. whenever he has time left, he works on his own gaming-site ( which will be launched somewhere within the next 4 months. He learned how to use ASP in less than 8 months - all thanks to 4Guys.

Article Information
Article Title: Enhancing Web Site Maintainability using Dynamic Templates
Article Author: Christiaan Verwijs
Published Date: Friday, February 04, 2000
Article URL:

Copyright 2019 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers