Mixing HTML and ASP code can be very difficult to maintain. Concatenating long strings in the code makes things difficult to read, and switching back and forth between ASP code and HTML can be very inefficient. Worst of all, you cannot use your HTML editor to edit the content presentation just as you usually do; you have to edit everything by hand. In this article we’ll see how to solve this issue using HTML templates.
A typical complex output
Sending emails through an ASP page has become pretty common. Typically, a user fills out some fields in a form, which then gets submitted to some ASP script responsible for reading the inputs and sends the email in some nice formatted way. A polite, and sometimes necessary feature, is to include the user’s inputs in the email. Consider the following common scenario for submitting and processing just eight form fields.
It soon becomes apparent that this is no way to edit your output. As the number of submitted inputs increases and your formatting becomes more complicated, your ASP code becomes hard to manage: hard to understand and hard to edit. This becomes even worse if you have to edit your output as HTML. What if you could use your favorite HTML editor to do this?
The solution: HTML templates

By HTML template I simply mean an HTML file with keywords of where we would like our content to be dynamic. Choose a word string that would not normally exist in a document. I chose REPLACE_, followed by what to replace. For example Name should be REPLACE_Name. We load the HTML template using the File System Object and simply read the contents of the file into a text stream. Then we search and replace our templated keywords with dynamic content.
Formatting an HTML Email with CDONTS
Let’s say you would like to send people their inputs in a nicely formatted HTML email. Using your favorite HTML editor, create an HTML template and save it as an HTML file. Call it template.htm. You might want to go into the source code and erase everything, except what’s between the of the document. You just want the content, and not the rest of the stuff that editors add by default. For example, when you create you file, your source code will look something like this:
Here’s an example of a template of mine:

In our ASP processing page we create a function that reads and properly outputs the template.
We can call this method from anywhere in our code to return a properly formatted HTML output. We can output it on the screen and/or use it in a process, like sending it as the Body of an email. Changing our original email code, we can now set the Body attribute of the email object to the ReadFile function:
I have showed how to use this technique to properly format an HTML email, but you can use it for any kind of output. The VBScript Replace function may not be the most effective way to search and replace words in a text stream, but I have tested it against hundreds of lines of code and it is pretty fast. You can use regular expressions instead to do that, and it would probably become faster depending on how big your HTML template is.
10 Responses to Formatting complex ASP output using HTML templates