To read the article online, visit

Using Template Files to Create Dynamic ASP Pages

By Ryan S.

Wow. Yet another tutorial. Ok. This one goes out to _r0b0x_, on IRC, who gave me the idea. If you are unfamiliar with the FileSystemObject, you should first read Reading/Writing Text Files Using ASP.

This tutorial revolved around using 1 ASP page to create a website of hundreds, maybe thousands of pages, based on a template file. I will discuss using 1 ASP page and a querystring, but in theory, you could create a component that would create individual pages instead of dealing with one, using the FileSystemObject. Who knows, I might write a tutorial on that someday.

First, I'll explain why this is on template files, and not just on one file using a database. Sometimes template files can offer a whole lot more functionality. For example, you could keep copying your ASP page, but change the template, to base your page on user themes. That way, you don't have to write who knows how many ASP pages for each theme.

Also, with a template file, you get much easier viewing of your code. This way, you (or your users) don't have to worry about viewing all the ASP code intermingled with your HTML, sometimes causing you to lose your train of thought. You can focus purely on the HTML

In my tutorial, I will be using a database (wow, is that a suprise by now). This database will be used for our extensive employee database, in our mock environment. Here is what we will be tracking for our template - Their Employee ID, Their first and last names, a picture of them, and a synopsis of their duties, with a date of when the picture was taken. Here is how our Access database would look -

FileName - myDatabase.mdb
Table - Employees
ID Autonumber
FullName Text - Maximum 100 characters
PicURL Text - Maximum 255 characters
Duties Memo Field
PicCaption Text - 50 Characters

As you can see, this is a VERY easy setup for a database. Sure it can be expanded, but I'll let you handle that. Now I'm assuming that you know the basics of databases, so I won't go to much in depth; the code presented in this article is very, very simplistic code.

Now that we have our database setup, we want to create our template file. This file will be the skeleton for every user. Here is what I decided upon. First, I opted for no tables, because I am lazy. Deal with it ;-). Also, I have no <html> or <head> because that will be used in our ASP page. So here is how it turned out:

	<img src="%img%" align="left"><p>Full Name: <b> %name% </b>
	<br>Description of duties: <br>%duties%
	<br>This picture was taken: %date%
	Employee ID: %empID%
Yep. That's all that I have setup for the code. Pretty simple template. Notice that we CAN use HTML tags in our template. Now we have to design our ASP page. This page will be in charge of reading the template and the database, and returning the page we desire. Here is what it will look like:

If Request.QueryString("EmpID") = "" Then
    Response.Redirect "Employee.htm"
End If

This code right here tells our ASP page that if the user hasn't selected an employee from the database, take them to the employee selection page. I'm not going to write code for that here, but in theory, you could make an ASP page to read off the employee information. Next, we create a connection if the user has supplied an ID:

	set cnADO = Server.CreateObject("ADODB.Connection")
	dsnDSN = "DSN=myDB"
	cnADO.Open dsnDSN
	set mySQL = "SELECT * FROM EMPLOYEES WHERE ID=" & Request.QueryString("EmpID")
	set rsTemp = cnADO.Execute(mySQL)

This is our database code. Simple and straight up. Not a whole lot of interpreting needed.

	'If we can't find the employee ID, redirect the
	' user to a not found page...
	If rsTemp.EOF then
	    Response.Redirect "notfound.htm"

This code checks to see if the employee is not found. If that is the case, then take them to our error page describing why.

	set fso = Server.CreateObject("Scripting.FileSystemObject")
	set ourtext = fso.GetFile("template.tmp")

	'Open a file for reading
	set ts = ourtext.OpenAsTextStream(1,-2)

	Do while not ts.AtEndOfStream
		myText = ts.readline
		myText = replace(myText, "%img%", rsTemp("picURL"))
		myText = replace(myText, "%name%", rsTemp("FullName"))
		duties = replace(rsTemp("duties"), vbCrLf, "<br>")
		myText = replace(myText, "%duties%", duties)
		myText = replace(myText, "%empID%", rsTemp("ID")
		myText = replace(myText, "%date%", rsTemp("PicCaption")
		Response.Write myText

	'Clean up...
	set ts = nothing
	set ourtext = nothing
	set fso = nothing

	set rstemp = nothing

	set cnADO = nothing


Wow. That's all our code. Seems simple, doesn't it? What it does is open our template file, and read each line sequentially. It replaces our coded strings (%img%, %name%, et cetera) with our actual fields. Also, it converts each line break in our duties to a <br>, to maintain formating. Very simple!

Well, there you have it. Simple way to make template files. Read's from a database, and offer tons of features. In theory, you could adapt this code to write text files, like I said, using the FileSystemObject. Then, just schedule the ASP page to execute, and you are set.

I hope this tutorial has helped clear up things. See you next time -
God Bless from Ryan S

Article Information
Article Title: - Using Template Files to Create Dynamic ASP Pages
Article Author: Ryan S.
Published Date: Sunday, June 27, 1999
Article URL:

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