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.
Dim strName Dim strEmail Dim strSubject Dim strMessage Dim strAddress Dim strPhone Dim strFax Dim strProfession Dim objEmail strName = Request.Form("Name") strEmail = Request.Form("Email") strSubject = Request.Form("Subject") strMessage = Request.Form("Message") strAddress = Request.Form("Address") strPhone = Request.Form("Phone") strFax = Request.Form("Fax") strProfession = Request.Form("Profession") Set objEmail = Server.CreateObject("CDONTS.NewMail") objEmail.From = strEmail objEmail.To = "firstname.lastname@example.org" objEmail.Subject = strSubject objEmail.Body = "Email sent at " & Now() & vbcrlf _ & "by " & strName & vbcrlf _ & "---------------------------------------" & vbcrlf & vbcrlf _ & "Address: " & strAddress & vbcrlf _ & "Phone: " & strPhone & vbcrlf _ & "Fax: " & strFax & vbcrlf _ & "Profession: " & strProfession & vbcrlf _ & "Message: " & strMessage & vbcrlf objEmail.Send Set objEmail = Nothing
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:
<html> <head> <title>Untitled</title> </head> <body> <em><!--template start; erase all above--></em> ... <em><!--template end; erase all below--></em> </body> </html>
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.
Function ReadFile(filename) Dim objFSO Dim objTStream Dim strText Const ForReading = 1 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objTStream = objFSO.OpenTextFile(Server.MapPath(filename), ForReading) strText = objTStream.ReadAll Set objTStream = Nothing Set objFSO = Nothing strText = Replace(strText, "REPLACE_Name", strName) strText = Replace(strText, "REPLACE_Email", strEmail) strText = Replace(strText, "REPLACE_Phone", strPhone) strText = Replace(strText, "REPLACE_Fax", strFax) '-- Notice that below I am replacing all line breaks with the HTML <br>. ' This applies to all form textboxes, where the user can enter line breaks ' and it achieves proper display of the contents in HTML. strText = Replace(strText, "REPLACE_Address", Replace(strAddress, vbcrlf, "<br>")) strText = Replace(strText, "REPLACE_Profession", strProfession) strText = Replace(strText, "REPLACE_Subject", strSubject) strText = Replace(strText, "REPLACE_Message", Replace(strMessage, vbcrlf, "<br>")) ReadFile = strText End Function
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:
... Set objEmail = Server.CreateObject("CDONTS.NewMail") objEmail.BodyFormat = 0 'CdoBodyFormatHTML objEmail.MailFormat = 0 'CdoMailFormatMime objEmail.From = strEmail objEmail.To = "email@example.com" objEmail.Subject = strSubject objEmail.Body = ReadFile("template.htm") objEmail.Send Set objEmail = Nothing
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.