Using the Templates

Now that we have defined our template, we can now look at how we will use it. To move from this template to a series of pages that provide a consistent look and feel to our users, we will need to do the following :

Our first task is to read the entire template file into our ASP as a variable, we can achieve this using some simple VBScript based on the samples from the MicroSoft Developer's Network (MSDN) website :

' create a link to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
' create a link to the file
ls_file_path = Server.MapPath("./template.html")
 
' open the file
Set ObjTemplateFile = objFSO.OpenTextFile(ls_file_path)
 
' read the entire file into a variable
template_html = ObjTemplateFile.ReadAll

We now have all of the template file in a variable called template_file. Now we need to replace our placeholders with actual data, so we can use the Replace method to do this :

Replace(template_html,"{pageTitle}","My Output Page")
Replace(template_html,"{navigationElements}","<a href='index.html'>Home</a><p><a href='http://www.greggriffiths.org'>my site</a>")
Replace(template_html,"{pageHeading}","This is my Test Page")
Replace(template_html,"{pageContent}","This is content of my sample page")

These calls replace our placeholder tags with the data that we have provided, all that we need to do then is to send the updated output to the client using a simple Response.Write as shown in the demonstration.

Naming our placeholders tags as we have done should ensure that we don't replace anything we shouldn't as long as we ensure that the placeholders don't appear in our template anywhere where we don't want them replaced. Although, if we want to show a placeholder tag and no have it replaced, we can simply use the HTML attributes for the curly braces, so that they show up in the output, but will not result in a match in our Replace job.

However, if we don't replace a placeholder with a value, then the placeholder tag will appear in output - as shown in this example. Thus, we need to be sure that we replace ALL of the placeholders in the template, even if we just replace them with a space or an empty string.

If we wanted to use this approach with more dynaic data such as the results of a database, we would need to pass in some value to identify which set of values we wanted to display in our output page, then we could get the data from the database. The following example uses a parameter - pageid - passed in the QueryString :

Dim dbConn
Dim SQLStatement
 
SQLStatement="SELECT * FROM pageinfo WHERE pageid=" & Request.QueryString("pageid")
 
' create a link to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
' create a link to the file
ls_file_path = Server.MapPath("./template.html")
 
' open the file
Set ObjTemplateFile = objFSO.OpenTextFile(ls_file_path)
 
' read the entire file into a variable
template_html = ObjTemplateFile.ReadAll
 
' connect to the DB
db_connect
 
' run the Query, putting the results into a results set
set results=dbConn.execute(SQLStatement)
 
' process the results
while not (results.EOF)
    Replace(template_html,"{pageTitle}",results("pageTitle"))
    Replace(template_html,"{navigationElements}",results("navigation"))
    Replace(template_html,"{pageHeading}",results("heading"))
    Replace(template_html,"{pageContent}",results("content"))
next
 
' disconnect from the DB
db_disconnect
 
' send the output back to the client
Response.Write(template_html)
 
sub db_connect
    Dim DSNName="mysite"
    Dim DBUserName="me"
    Dim DBPassword="you"
 
    ' Create a connection object
    set dbConn = server.createobject("ADODB.connection")
 
    ' Open a connection using the following criteria - DSN Name, UserName, Password
    dbConn.open DSNName, DBUserName, DBPassword
end sub
 
sub db_disconnect
    dbConn.close
end sub

Thus, we can now generate a wide selection of pages from a single easy to amend template. This template can use static or database driven data to populate the output.
Website Designed by Adservio Consulting      Bookmark and Share Valid HTML 4.01 Strict    Valid CSS!    Level A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0