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:
- Thank you!
- Dear <<< name >>>,
- thank you for your registration with
- 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:
- <!--#include virtual="textTemplate.asp"-->
- <%
- set t = new TextTemplate
- with t
- .filename = "/emails/thankyou.txt"
- .add "name", "Jack Kett"
- subject = .getFirstLine()
- body = .getAllButFirstLine()
- end with
- %>
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 ;)):
- Your purchase [number]
- Dear [name],
- thank you for purchasing with phat ecommerce.
- These are the articles you ordered:
- [BLOCK article]
- Name: [name]
- Amount/Price: [amount] / [price]
- [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:
- <!--#include virtual="textTemplate.asp"-->
- <%
- set t = new TextTemplate
- with t
- .filename = "/emails/purchase.txt"
- .placeHolderBegin = "["
- .placeHolderEnd = "]"
- .add "number", "089389"
- .add "name", "Jack Kett"
- set block = new TextTemplateBlock
- block.addItem(array("name", "iPhone", "amount", 2, "price", 299))
- block.addItem(array("name", "iRock", "amount", 1, "price", 79))
- .add "article", block
- subject = .getFirstLine()
- body = .getAllButFirstLine()
- end with
- %>
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:
TextTemplate documentation
NOTE: TextTemplate class is part of the ajaxed library now!
check ajaxed library for a newer version…
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.
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.
Pingback: Web Dev Bros » Blog Archive » ajaxed 1.0 released
it is not working !! even your sample …
Thank you!
Dear <<>> ,
thank you for your registration with
out website.
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
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 = "#]“
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.-