問題描述
您好,我有一個需要編輯的 ASP 腳本.實際上我需要重新設置它發送的電子郵件的樣式,所以我需要從中編輯 HTML.
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.
問題是html(來自asp文件)在每一行都有
The problem is the html (from the asp file) has on every row
HTML = HTML & ="
在其中(加上其他一些更改).我需要從該 ASP 中獲取 HTML 代碼,去掉開頭的 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).
然后,我用 HTML 重新設置頁面的樣式,然后我需要將它轉換回來,以便我可以將它集成到 ASP (基本上再次引入雙 '"' 和其他東西).
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).
是的,我可以直接從 ASP 編輯 HTML,但我不知道它的外觀,因為我無法運行腳本 (它需要來自服務器的其他文件,我不需要't 有權訪問).
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).
問題:
有更好的方法嗎?
某種方式可以直接預覽我在 ASP 中所做的事情.或者也許是一個工具,可以讓我從 ASP HTML 移動到 HTML 并更快地返回.
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.
推薦答案
作為 @steve-holland 提及創建模板是避免代碼中所有煩人的 HTML 字符串并使更改布局變得輕而易舉的好方法.
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.
我過去曾自己處理過 HTML 模板腳本,通常我會構建一個 Scripting.Dictionary
,其中包含我將在模板中替換的鍵值對.
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
用法:
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)
示例main.htm
結構:
<!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>
為什么使用 ADODB.Stream
而不是 Scripting.FileSystemObject
?;
Why use a ADODB.Stream
instead of the Scripting.FileSystemObject
?;
您可以控制返回的
Charset
,如果需要,甚至可以從一個字符集轉換為另一個字符集.
You can control the
Charset
being returned and even convert from one to another if you need to.
如果模板特別大,您可以使用 Read()
方法以特定的緩沖區大小將內容流式傳輸,以提高讀取的性能.
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.
這篇關于如何以更好的方式從 ASP 編輯 html?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!