把html代碼寫入到文件中然後生成.html格式的文件
<%
filename=test.htm
if request(body)<> then
set fso = Server.CreateObject(Scripting.FileSystemObject)
set htmlwrite = fso.CreateTextFile(server.mappath(filename))
htmlwrite.write <html><head><title> request.form(title) </title></head>
htmlwrite.write <body>輸出Title內容: request.form(title) <br /> 輸出Body內容: request.form(body) </body></html>
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name=form method=post action=>
<input name=title value=Title size=26>
<br>
<textarea name=body>Body</textarea>
<br>
<br>
<input type=submit name=Submit value=生成html>
</form>
2、但是按照上面的方法生成html文件非常不方便,第二種方法就是利用模板技術,將模板中特殊代碼的值替換為從表單或是數據庫字段中接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成HTML文件.這種技術採用得比較多,大部分的CMS都是使用這類方法.
template.htm ' //模板文件
<html>
<head>
<title>$title$ by jzxue.com</title>
</head>
<body>
$body$
</body>
</html>TestTemplate.asp '// 生成Html
<%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 創建文件系統對象
Set fso=Server.CreateObject(Scripting.FileSystemObject)
'// 打開網頁模板文件,讀取模板內容
Set htmlwrite=fso.OpenTextFile(Server.MapPath(Template.htm))
strOut=f.ReadAll
htmlwrite.close
strTitle=生成的網頁標題
strContent=生成的網頁內容
'// 用真實內容替換模板中的標記
strOut=Replace(strOut,$title$,strTitle)
strOut=Replace(strOut,$body$,strContent)
'// 創建要生成的靜態頁
Set htmlwrite=fso.CreateTextFile(Server.MapPath(test.htm),true)
'// 寫入網頁內容
htmlwrite.WriteLine strOut
htmlwrite.close
Response.Write 生成靜態頁成功!
'// 釋放文件系統對象
set htmlwrite=Nothing
set fso=Nothing
%>