生成sHTML静态页面的详细过程及分析

最近在做调用SHtml模板生成静态页面的项目二次开发,找了点相关的资料。自己也在研究

生成sHTML静态页面,同时调用模板,便于修改。
过程如下:
<%
Action=Trim(Request("Action"))
'变量ACTION=1为新增,ACTION=0为修改
title=Trim(Request("title"))
content=Trim(Request("content"))
'....更多内容来源

if Action=0 then

'修改sHTML文件
id=Trim(Request("id"))
'指定修改的文章
response.buffer=true
Response.Expires=0
'清除缓存
set rs=server.createobject("adodb.recordset")
sql = "Select url From Docs where id="&id
rs.Open sql,conn,1,1
if not(rs.eof and rs.bof) then
filepath =rs("url")
'读取文章路径
end if
StaticFilePath = filepath
'设定另一变量后用。
filepath=replace(filepath,".shtml","")
'转换文件名后缀
filepath="/a/"&filepath
'得到完整文件路径
else

'生成新sHTML文件

   d=year(date())&month(date())&date(date())
   t=hour(now())&minute(now())&second(now())
   randomize
   '使用系统计时器来初始化乱数产生器
   t=rnd(t)

StaticFilePath= d &"/"& d & t &".shtml"
folder = "/a/"& d &"/"
'存储在数据库中的路径,文件夹a为自定
filepath = folder&t
'得到完整文件路径
end if


'保存数据到数据库
set rs=server.createobject("adodb.recordset")
sql = "Select * From ......"
'.............
'过程不是这部份的,省略

ContentStr=split(Content,"|||")
'查找内容中的分页符
PageNumber=ubound(ContentStr)
For p = 0 to ubound(ContentStr)

Dim fso,fin
'创建文件系统对象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Model="/a/Template/index.shtml"
'sHTML模板文件路径
Set fin=fso.OpenTextFile(Server.MapPath(""&Model&""))
mb_code=fin.ReadAll
fin.close
'打开模板代码,

' REM 转换其中特殊代码转变为接受值 开始
mb_code=replace(mb_code,"$$Pagename$$",Pagename)
mb_code=replace(mb_code,"$$Title$$",Title)
mb_code=replace(mb_code,"$$Posttime$$",now())
mb_code=replace(mb_code,"$$PinlunContent$$",PinlunContent)
mb_code=replace(mb_code,"$$Content$$",ContentStr(p))

if PageNumber>0 then
If p = 0 and p < PageNumber Then
'分页数大于一时
'生成图片翻页链接 开始
MyPagerTable="<div align=center><a href=""$UrlDown$""><img src=""/images/page_down.gif"" width=""69"" height=""21"" border=""0"" /></a></div>"
MyPagerTable=replace(MyPagerTable,"$UrlDown$",fname&"_"&p+1&".shtml")
ElseIf p = 1 and p < PageNumber Then
MyPagerTable="<div align=center><a href=""$UrlUp$""><img src=""/images/page_up.gif"" width=""69"" height=""21"" border=""0"" /></a> <a href=""$UrlDown$""><img src=""/images/page_down.gif"" width=""69"" height=""21"" border=""0"" /></a></div>"
MyPagerTable=replace(MyPagerTable,"$UrlUp$",fname&".shtml")
MyPagerTable=replace(MyPagerTable,"$UrlDown$",fname&"_"&p+1&".shtml")
Elseif p = 1 and p = PageNumber Then
MyPagerTable="<div align=center><a href=""$UrlUp$""><img src=""/images/page_up.gif"" width=""69"" height=""21"" border=""0"" /></a></div>"
MyPagerTable=replace(MyPagerTable,"$UrlUp$",fname&".shtml")
Elseif p = PageNumber Then
MyPagerTable="<div align=center><a href=""$UrlUp$""><img src=""/images/page_up.gif"" width=""69"" height=""21"" border=""0"" /></a></div>"
MyPagerTable=replace(MyPagerTable,"$UrlUp$",fname&"_"&p-1&".shtml")
else
MyPagerTable="<div align=center><a href=""$UrlUp$""><img src=""/images/page_up.gif"" width=""69"" height=""21"" border=""0"" /></a> <a href=""$UrlDown$""><img src=""/images/page_down.gif"" width=""69"" height=""21"" border=""0"" /></a></div>"
MyPagerTable=replace(MyPagerTable,"$UrlUp$",fname&"_"&p-1&".shtml")
MyPagerTable=replace(MyPagerTable,"$UrlDown$",fname&"_"&p+1&".shtml")   
End If
'生成图片翻页链接 结束
mb_code=replace(mb_code,"$$MyPager$$",MyPagerTable)
'转换分页代码
else
mb_code=replace(mb_code,"$$MyPager$$","")
'只有一页时,分页代码为空
end if

'开始生成sHTML页面
On Error Resume Next
'容错处理
if Action=0 then
Set fs = Server.CreateObject("Scripting.FileSystemObject")
   if p=0 then
   File = Server.MapPath(filepath&".shtml")
   else
   File = Server.MapPath(filepath&"_"&p&".shtml")
   end if
On Error Resume Next
' 若有错误依然向下执行
fs.DeleteFile File, True
end if
fso.CreateFolder(Server.MapPath(folder))
if p=0 then
Set fout = fso.CreateTextFile(Server.MapPath(filepath&".shtml"))
else
Set fout = fso.CreateTextFile(Server.MapPath(filepath&"_"&p&".shtml"))
end if
fout.WriteLine mb_code
fout.close
next

Response.Write("完成任务。")
%>

关于模板文件index.shtml的内容按HTML设计好即可,注意要转换的内容中使用转换标识符。
shtml文件可使用include file包含另一文件,但不能运行ASP等动态内容

posted @ 2010-07-23 09:23  pig_wang  阅读(1478)  评论(0编辑  收藏  举报