Colorful Life2010

asp template 1.5
Weather:多云,南风4-5级,最低气温18 ℃

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)
  • 历史上的今天: [2007/09/15]包二奶来了
    [2006/09/15]秀照片了,不多

    [asp template 1.5]的回复

    Post a Comment~