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 the asp file) has on every row
HTML = HTML & ="
in it (plus some other changes). I need to take the HTML code from that ASP, get rid of the beginning html = html
part, edit the double ""
and convert them to a single "
(I need to do that one by one, because the variables also have quotes in them).
Than, I restyle the page with HTML and after that I need to convert it back so I can integrate it in ASP (basically introduce the double '"' again and stuff).
Yeah, I could edit the HTML from the ASP directly, but I don't know how it might look, because I can't run the script (it needs other files from the server, which I don't have access to).
The question:
Is there a better way of doing this?
Some way of previewing what I'm doing in ASP directly. Or maybe a tool that let's me move from ASP HTML to HTML and back faster.
I sure know that what I'm doing right now is quite dumb, so there must be a better way.
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).
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.