Hello I have an ASP script that I need to edit. Actually I need to restyle the email that it sends, so I need to edit the HTML from it.
The problem is the html (from
As @steve-holland mentions creating a template is a great way to avoid all the annoying HTML strings in the code and makes changing layouts a breeze.
I've worked on HTML templating scripts myself in the past, usually I build a Scripting.Dictionary
that contains the key value pairs I will be replacing inside the template.
Function getHTMLTemplate(url, params)
Dim stream, keys, html, idx
Set stream = Server.CreateObject("ADODB.Stream")
With stream
.Type = adTypeText
.Charset = "utf-8"
Call .Open()
Call .LoadFromFile(url)
html = .ReadText(adReadAll)
Call .Close()
End With
Set stream = Nothing
keys = o_params.Keys
For idx = 0 To UBound(keys, 1)
html = Replace(html, keys(idx), params.Item(keys(idx)))
Next
Set keys = Nothing
Set params = Nothing
getHTMLTemplate = html
End Function
Usage:
Dim params, html
Set params = Server.CreateObject("Scripting.Dictionary")
With params
.Add("[html_title]", "Title Here")
.Add("[html_logo]", "/images/logo.gif")
'... and so on
End With
html = getHTMLTemplate(Server.MapPath("/templates/main.htm"), params)
Call Response.Write(html)
Example main.htm
structure:
<!doctype html>
<html>
<head>
<title>[html_title]</title>
<link rel="stylesheet" type="text/css" href="/styles/main.css" />
</head>
<body>
<div class="header">
<img src="[html_logo]" alt="Company Name" />
</div>
</body>
</html>
Why use a ADODB.Stream
instead of the Scripting.FileSystemObject
?;
You can control the Charset
being returned and even convert from one to another if you need to.
If the template is particular large, you can stream the content in using the Read()
method with a specific buffer size to improve the performance of the read.
You could create a html template file with some placeholders in it, read it in, replace the placeholders and then use it in your email. Saves you having to keep messing about building up the html using variables. This previous answer has some more details about a possible solution (with code examples).