-
- asp template 1.5
- Weather:多云,南风4-5级,最低气温18 ℃
- 2005-09-15
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 toAddToken
).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 tokendate
is found in the template, it will be replaced with the output ofFormatDateTime(Now(), 3)
.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.
Parsing Templates
There are four ways to parse templates using the class. You can specify a file as template with theTemplateFile(filespec)
method and then call eitherparseTemplateFile
to output the template orgetParsedTemplateFile
, which will return a string with the output of the parsing. Alternatively you can pass a template via a string to class by using eitherparseTemplateString(string)
orgetparsedTemplateString(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 calledprocessRows
(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 templateOpenTag(string)
?change the default opening tagCloseTag(string)
?change the default closing tagCI(Boolean)
?change the case sensitivity of the tokens (T=Sensitive, F=non-sensetive)
Methods
AddToken(string, integer, variant)
?add a token to the classDelToken(string)
?delete a token from the classRemoveAllTokens
?delete all tokens from the classparseTemplateFile
?parse the template and display the results immediatelyparseTemplateString(string)
?parse the supplied string and display the results immediatelygetParsedTemplateFile
?parse and return the templategetParsedTemplateString(string)
?parse and return the supplied stringfileExists(filespec)
?check whether a file existsloadFile(filespec, integer)
?load and either display or return a file (see source for details)
Happy Programming!
Attachments:
- Download the ASP Template 1.5 library (in ZIP format)
- String Variable (
-
Views(5241) | Comments(0) |
In:
web develop
|

