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

Process Template

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:

Template Example

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

  • Helen Huxford

    I have a question. I already have a form set up. Can I just include this code in my ASP file?

    • Evagoras Charalambous

      Can you please go into more detail of what it is you are trying to do, so that I may better answer your question?

  • Ben Bagg

    Hi Evagoras,

    I have made a HTML form with a generator for my jobs intranet and I cannot get it to work with an ASP file I was wondering if you could show me why? and help me fix the issue, here is a little example.

    (part of HTML)
    Meeting Room Booking
    Book meeting rooms and equipment here.

    Full Name

    Then I try to connect just the First name and Last name to my ASP file to test that the form is working, it will open the file but will not do anything but give me a blank page, here is my ASP Code.

    PS. Please Email me if you can help! ben.bagg@lantra.co.uk

  • Ben Bagg

    sorry it will not show the code.

  • Deepak

    I need to add enter ASc values Char(13) and char(10) in a paragraph. Some one help me.

    • Evagoras Charalambous

      If you mean to replace file line changes with HTML BR codes, then you can simple do:
      Replace(strAddress, vbcrlf, "<br>")

  • franklin Aquino

    the page “processform.asp” is giving an error and can not find. you can take a look? grateful

    • Evagoras Charalambous

      @franklin Aquino,

      What is the exact error you are getting? Make sure you place the “template.htm” file inside the same folder so that it will be able to find it, read and parse it.

  • TEST

    This website was… how do you say it? Relevant!
    ! Finally I have found something that helped me. Kudos!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.