To read the article online, visit http://www.4GuysFromRolla.com/webtech/061402-1.shtml

An ASP Template Class (version 1.5)

By James Q. Stansfield


Introduction
Almost two years ago I wrote a little piece of VBScript that allowed for the separation of HTML content from ASP code. Essentially it allowed a Web site's content-writer(s) to write the content while the graphic designers could layout the HTML and images, specifying where the content should be "plugged in." The nice thing about this is that you can allow users to customize their Web site experience, or brand your content on other Web sites, with absolutely no need to alter the content portion. The code I wrote back then to accomplish this feat was neat and compact and did the job. However, when originally submitted to 4Guys, Scott Mitchell suggested that the code would be much easier to use if it were encapsulated in a VBScript class. I acknowleged that wisdom even though I didn't know much about VBScript classes, but I quickly learned and am a much better developer for it. (Thanks Scott. By the way, to learn more about VBScript classes be sure to read: Using Classes in VBScript!)

Since then, I've had the opportunity to expand the code greatly and have been using a version similar to what I'm presenting today in a few large scale production Web sites. For those of your who are unclear what the separation of HTML content from ASP code means, or how "templates" can be used to foster such good design practices, you should read my original article from September of 2000: An ASP Template Class.

Tokens And Variable Types
Tokens
As presented before, the key to separating the content from code is producing templates that have placeholders (tokens) for the dynamic data within the HTML and other static text. I've always used [% and %] to delimit my tokens from the rest of the code. Tokens are by default case insensitive, so [%TokenName%] is the same as [%tokenname%]. Most characters are allowable, however I've not tested much beyond the standard alpha-numeric set.

Token Types
Token types are a new addition to the template class. Traditionally in template systems there is only one type of token, one that is replaced by a variable when the template is parsed. However there are times when you may want a token to point to another file such as a header or footer template, rather than having the token solely for variable replacement. Thus we now have to define our token type when creating a token (the second parameter to AddToken).

This is accomplished by the AddToken method:

objTemplate.AddToken "date", STRINGVARIABLE, FormatDateTime(Now(), 3)

You may be wondering what a token type of STRINGVARIABLE means. It signifies a simple variable, thus wherever the token date is found in the template, it will be replaced with the output of FormatDateTime(Now(), 3).

Here is the full list of the token types:

  1. String Variable (STRINGVARIABLE) - A simple string variable is inserted where the token is in the template.
  2. Include and Parse File (INCLUDEANDPARSE) - This effectively an include file that gets passed through the class. Useful for header files or repeating HTML elements.
  3. Include File (INCLUDEANDNOPARSE) - This is the same as above but it does not get parsed. (Why do this work if there aren't going to be any tokens in the file?) Useful for including large passages of text, etc.
  4. Reserved for future use
  5. Parse String (PARSESTRING) - This is a string that contains tokens that need to be parse through the class.
  6. Parse Array (PARSEARRAY) - This expects a single dimensioned array.

Parsing Templates
There are four ways to parse templates using the class. You can specify a file as template with the TemplateFile(filespec) method and then call either parseTemplateFile to output the template or getParsedTemplateFile, which will return a string with the output of the parsing. Alternatively you can pass a template via a string to class by using either parseTemplateString(string) or getparsedTemplateString(string) to either display or return the results as required.

Bringing It All Together
I've put together three examples to illustrate the effectiveness of the class, all of which are included in the ZIP file at the end of this article. There are also live demos for these various templates. The three templates are:

  • Simple.asp - a very basic template [View a Live Demo!]
  • Complex.asp- a basic template demonstrating variable types 2 and 3 [View a Live Demo!]
  • Reallycomplex.asp - an advanced template illustrating how to apply different "skins" to a Web site [View a Live Demo!]

Applying Templates To RecordSets
Included with the template class is a function called processRows (found in /includes/functions.asp) that demonstrates some of the functionality of the class. This function will loop through a recordset and will apply each row in the recordset to a template passed to it. For example:

Response.Write(objRS, "<tr><td>[%id%]</td><td>[%lname%], [%fname%]</td></tr>")

The above example would apply the template to each row in the recordset objRS, producing results such as:

<tr><td>1</td><td>Stansfield, James</td></tr>
<tr><td>2</td><td>Stansfield, Lisa</td></tr>
<tr><td>3</td><td>Stansfield, Stephanie</td></tr>
<tr><td>4</td><td>Smith, Bobby</td></tr>
<tr><td>5</td><td>Johanssen, Sven</td></tr>

This is just an example of the power of the template class. This function could easily be modified to alternate the background color on alternating rows. In the past, I've used this function to return an option list for a select box.

Public Properties And Methods
Here is the full list of public properties and methods in the class.

Properties

  • TemplateFile(filespec) assign a file to be used as a template
  • OpenTag(string) change the default opening tag
  • CloseTag(string) change the default closing tag
  • CI(Boolean) change the case sensitivity of the tokens (T=Sensitive, F=non-sensetive)

Methods

  • AddToken(string, integer, variant) add a token to the class
  • DelToken(string) delete a token from the class
  • RemoveAllTokens delete all tokens from the class
  • parseTemplateFile parse the template and display the results immediately
  • parseTemplateString(string) parse the supplied string and display the results immediately
  • getParsedTemplateFile parse and return the template
  • getParsedTemplateString(string) parse and return the supplied string
  • fileExists(filespec) check whether a file exists
  • loadFile(filespec, integer) load and either display or return a file (see source for details)

Happy Programming!

By James Q. Stansfield


Attachments:

  • Download the ASP Template 1.5 library (in ZIP format)


  • Article Information
    Article Title: An ASP Template Class (version 1.5)
    Article Author: James Q. Stansfield
    Published Date: Friday, June 14, 2002
    Article URL: http://www.4GuysFromRolla.com/webtech/061402-1.shtml


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