Template component for classic ASP

By | June 28

You might know it from other plattforms but hardly seen it in classic ASP apps: Templates. Some time ago I have written a Template component which i recently updated and decided to make it publicly available. It allows you to load a template file and replace its placeholders with actual values. It comes up with featues like e.g. repeating blocks
Within the applications i develop i am seperating the email content from the actual source code. This results in advantages like easy maintenance and clear code. For this I am using my template component called TextTemplate which allows me to load a given file (template), replace all placeholder and assign the resulted string(s) to the email. Strings (plural) because I am also storing the emails subject in the template to move as much as possible into the template (but still keep the template component generic). Lets say we have the following template for our email saved as thankyou.txt:

  1. Thank you!
  2. Dear <<< name >>>,
  3. thank you for your registration with
  4. out website.

The templates first line is the emails subject and rest the body. It contains one placeholder called “name” which we wanna replace by the name of the registered user (Important: there is a space between the begin and end tag of the placeholder!). Now we want to load this template and send an email using its parsed content. The TextTemplate solves it the following way:

  1. <!--#include virtual="textTemplate.asp"-->
  2. <%
  3. set t = new TextTemplate
  4. with t
  5.   .filename = "/emails/thankyou.txt"
  6.   .add "name", "Jack Kett"
  7.   subject = .getFirstLine()
  8.   body = .getAllButFirstLine()
  9. end with
  10. %>

This example does not really sends an email (this is up to you how to manage this) but it assigns the body and subject (grabbed from the template) into two variables named the same. As you can see the add() method adds an actual value for the placeholder name and replaces it when you request the content using getFirstLine(), getAllButFirstLine() or returnString() to get the whole content. Thats a simple example which demonstrates a nice seperation of code and the email content. You could also create a thankyou.html file which would hold the template as html and indicate that the email needs to be sent as HTML.

Lets take a look which other features the component offers:

  • block support (template within a template)
  • case unsensitive
  • fully supports UTF8 templates (by default the templates must be saved as UTF8)
  • customization of placeholder tags (lets you define the begin and end of a placeholder)
  • creating, updating and deleting templates programmatically
  • clean parse (removes all unused placeholders after parsing)

All features are straightforward to use but the block support might be the most trickiest one. Thats why i want to illustrate its usage and intension. Lets suppose we have the following template which is send as a confirmation for an order within our phat ecommerce business application (surely hacked with classic ASP ;)):

  1. Your purchase [number]
  2. Dear [name],
  3. thank you for purchasing with phat ecommerce.
  4. These are the articles you ordered:
  5. [BLOCK article]
  6. Name: [name]
  7. Amount/Price: [amount] / [price]
  8. [ENDBLOCK article]

At first sight you see different “tags” for the placeholders ([ and ]). Those are here to demonstrate the conifguration of the component. The first line again represents the subject and as you can see its also possible to use a placeholder within this line. The rest is quite similar to the first example apart from the BLOCK part. This block represents a recurring template within the template. In this case the reason is because the user might have bought more different articles. Okay, so how do we parse the template now. The following snippet clears the question:

  1. <!--#include virtual="textTemplate.asp"-->
  2. <%
  3. set t = new TextTemplate
  4. with t
  5.   .filename = "/emails/purchase.txt"
  6.   .placeHolderBegin = "["
  7.   .placeHolderEnd = "]"
  8.   .add "number", "089389"
  9.   .add "name", "Jack Kett"
  10.   set block = new TextTemplateBlock
  11.   block.addItem(array("name", "iPhone", "amount", 2, "price", 299))
  12.   block.addItem(array("name", "iRock", "amount", 1, "price", 79))
  13.   .add "article", block
  14.   subject = .getFirstLine()
  15.   body = .getAllButFirstLine()
  16. end with
  17. %>

The only two differences are that the placeholder tags are changed to [ ] and that one placeholder is represented by a TextTemplate block object. This object holds the date for a block defined in a template. So first its necessary to create a block and add the items (data) to it. Then the block can be added as the value of a placeholder as it can be done with a common string.

Hope you have fun using this component and I am happy about every single feedback i get. This helps me improving the interface of the components and keep the as intuitive as possible. Here are the necessary links:

Download TextTemplate 1.2

TextTemplate documentation

NOTE: TextTemplate class is part of the ajaxed library now!
check ajaxed library for a newer version…

7 comments on “Template component for classic ASP

  1. Hi Michal!

    It looks great, I work a lot with sending data from my site via email to users. and this script will help me so much!
    so thanks!

    my site is in hebrew so i’ll get back to you with feedback how it worked in a rtl language.

  2. Hi there! Nice blog posting about Template component for classic ASP. I would have to agree with you on this one. I am going to look more into html templates. This Tuesday I have time.

  3. Pingback: Web Dev Bros » Blog Archive » ajaxed 1.0 released

  4. it is not working !! even your sample …

    Thank you!
    Dear <<>> ,
    thank you for your registration with
    out website.

  5. Excuse me !

    I changed the default <<>> tag to <> and it is working
    but some times i gut run time error when i was testing new tags like [*name*]

    Microsoft VBScript runtime error ’800a1399′

    Syntax error in regular expression

    /asp3/textTemplate.asp, line 215

  6. I found the solution :

    ‘[' and ']‘ are using in regular expression as default,then we need to use to determine we want to use them as a tag

    .placeHolderBegin = “[#"
    .placeHolderEnd = "#]“

  7. I have downloaded TextTemplate 1.2 and it is working fine for me. I have doubts about the licensing, in the source code for the classes I can see:

    GAB_LIBRARY Copyright (C)
    License refer to license.txt

    but there is no “license.txt” file in the root or anywhere else. Can you please let me know what the licensing conditions for the component are?

    Thanks.-