An ASP Template Class (version 1.5)By James Q. Stansfield
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
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
%] 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 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
This is accomplished by the
You may be wondering what a token type of
It signifies a simple variable, thus wherever the token
date is found in the
template, it will be replaced with the output of
Here is the full list of the token types:
- String Variable (
STRINGVARIABLE) - A simple string variable is inserted where the token is in the template.
- Include and Parse File (
INCLUDEANDPARSE) - This effectively an include file that gets passed through the class. Useful for header files or repeating HTML elements.
- 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.
- Reserved for future use
- Parse String (
PARSESTRING) - This is a string that contains tokens that need to be parse through the class.
- Parse Array (
PARSEARRAY) - This expects a single dimensioned array.
There are four ways to parse templates using the class. You can specify a file as template with the
TemplateFile(filespec) method and then
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
getparsedTemplateString(string) to either display or return the results as
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:
The above example would apply the template to each row in the recordset
results such as:
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.
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)
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)