When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
Published: Wednesday, September 13, 2000

An ASP Template Class, Part 2

By James Q. Stansfield


  • Read Part 1

  • In Part 1 we looked at separating content and layout using cascading style sheets, include files, and custom functions. In this part, we'll examine using templates to keep content and layout separate!

    - continued -

    '

    Templates
    Recently I've started working with template files, which in my opinion are much better than the previous two methods. With template files, the designer can work with a single HTML file. The designer can add the appropriate 'tags' where needed or leave that up to the developer. Below you can see an example of a template file:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
      <title>[%TITLE%]</title>
    </head>
    <body>
    <table border="0" width="785" align="center"
              cellspacing="0" cellpadding="0" class="largetable">
    <tr>
      <td rowspan="2" height="100%" width="150" valign="top" align="right">
             [%MENU%]
      </td>
      <td valign="top" width="635" class="caption" align="center">
             [%QUOTE%]
      </td>
    </tr>
    <tr>
      <td valign="top" bgcolor="#b0c4de" width="635" class="layout">
             [%CONTENT%]
      </td>
    </tr>
    <tr>
      <td colspan="2" width="785" align="center" class="caption">
             [%COPYRIGHT%]
      </td>
    </tr>
    </table>
    </body>
    </html>
    

    As you can see by the example this is a standard HTML file the only difference being that there are a few tags that are bounded by [% and %]. These are the tags that will be replaced by dynamic content. Please note that you are not limited to the number of tags you can have on a given page, or the number that can reside on a single line.

    I've written a class that will take this, or any template file, and parse it with user-defined values stored in a Dictionary object. Below is the ASP file that creates an instance of the class, defines the tags and their values and generates the HTML.

    <%
    ' index.asp
      option explicit
    %>
    <!--#INCLUDE FILE="parse_cls.asp"-->
    
    <%
      'Declare our variables
      dim g_oPageGen
    
      'Cerate the class object
      set g_oPageGen = New parseTMPL
    
      'Set the template file
      g_oPageGen.TemplateFile = "template.html"
    
      'Add some custom tags to the dictionary
      g_oPageGen.AddToken "title", "Template Example"
      g_oPageGen.AddToken "copyright", "This is mine! All mine!"
      g_oPageGen.AddToken "quote", _
          """Tell Jabba I've got his money!""<br>--Han Solo, Star Wars 1977"
      g_oPageGen.AddToken "menu", _
          "Home Page<br>News Page<br>Link Page"
      g_oPageGen.AddToken "content", _
          "Welcome To My Home Page... Yadda Yadda Yadda!"
    
      'Generate the page
      g_oPageGen.GenerateHTML
    
      'Destroy our objects
      set g_oPageGen = nothing
    %>
    

    This example only loads one page and doesn't address the needs of most websites. However, for large multiple page sites you could put a default set of tags into an include file that is loaded on every page, then you need only specify what template file to load and add any page specific tags before using the GenerateHTML method. The parseTMPL class resides in the parse_cls.asp file. I won't get into it in this article as the file itself is documented. Download the accompanying files and test out the class for yourself (or try out the on-line live demo).

    Conclusion
    Using templates to aid in the delivery of your website is most valuable when the time comes to change the look & feel of your existing site. Simply change a template file that controls the layout and style specific HTML code is much easier than traversing multiple files of spaghetti code line by line.

    Future Enhancements
    The class has room for improvement:

      1. Error handling needs to be added.
      2. The class is limited to returning a string or variable for any tag.

    In a future article I will expand upon the class and it's capabilities and show how this can be used in the creation of a portal site. I will also show how mini-templates can be used within a larger template for even greater functionality!

    Happy Programming!

  • James Q. Stansfield


    Attachments:

  • Run the live demo!
  • Download the template class code (in ZIP format)

  • Software Developer / Programmer - Distributed Systems (NYC)
    Next Step Systems
    US-NY-New York

    Justtechjobs.com Post A Job | Post A Resume


    ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article