思路:
1。ASP根据模板生成静态页。栏目部分,页内广告为经常变动部分,但不想用js或〈iframe〉,因为不利于搜索,也不想每次改动都重新生成,所以生成.shtml,方便包含静态页,有利于搜索引擎抓取。
2.文章自动分页,分页处说明当前第几页,共多少页,最后页面末尾自动加“(全文完)”
3.文内自动加广告。为美观,文章分页内部如有图片则不加文内广告,纯文字页自动加文内广告。
方法:
第一步:制作头部静态页(tou.html),文内广告静态(ad.html)
第二步:制作模板页。(ptemp.html)
1.注意:模板页头部部分在静态页中这样写。
〈body〉
〈!--头部部分--〉
〈!--include file=“/tou.htm“〉 (菜单经常变动部分)
〈!--其他部分--〉
〈/body〉
第三步:生成.shtml静态页
函数:
〈%
’读取模板页函数
Function LoadFile(ByVal File)
Dim objStream
On Error Resume Next
Set objStream = Server.CreateObject(“ADODB.Stream“)
With objStream
.Type = 2
.Mode = 3
.Open
.LoadFromFile Server.MapPath(File)
If Err.Number〈〉0 Then
response.write(“〈script〉alert(““错误!模板页不存在!!““);history.go(-1);〈/script〉“)
Err.Clear
Response.End
End If
.Charset = “utf-8“
.Position = 3
LoadFile = .ReadText
.Close
End With
Set objStream = Nothing
End Function
’随机广告时要用的函数
Function CheckTheChar(TheChar,TheString)
’TheChar=“要检测的字符串“
’TheString=“待检测的字符串“
if inStr(TheString,TheChar) then
for n =1 to Len(TheString)
if Mid(TheString,n,Len(TheChar))=TheChar then
StrNum=StrNum+1
End if
Next
else
StrNum=0
end if
CheckTheChar=StrNum
End Function
%〉
生成过程
〈%
’调用数据库的过程不写了。总之得到 添加日期rs(“addtime“) 文章内容rs(“acontent“),文章名rs(“htmname“) (.shtml结尾)
’文内广告的静态页,用include方法包含,换广告时只换ad.html页内容全站静态页全换,这种对任何广告联盟的广告投放来说完全不违规,不信等生成完了就可以知道原因了。
arad=“〈!--#i nclude file=“&“““/ad.html“““&“--〉“
’创建html目录下格式如/html/2007-10/
folder=“/html/“
myyear=year(rs(“addtime“))
mymonth=month(rs(“addtime“))
addfolder=folder&myyear&“-“&mymonth&“/“
’创建目录
Set fso = Server.CreateObject(“Scripting.FileSystemObject“)
if Not fso.FolderExists(Server.MapPath(folder))then
fso.CreateFolder(Server.MapPath(folder))
end if
if Not fso.FolderExists(Server.MapPath(addfolder))then
fso.CreateFolder(Server.MapPath(addfolder))
end if
set fso=nothing
fso.close
’读取模板并放到变量 artempcode
artempcode=LoadFile(“/ptemp.html“)’用LoadFile函数
’文章内容放临时变量
tempcode=rs(“acontent“)
’文章中手动加分页符[ NextPage ],这里分割文章内容到分页
ptemp=split(tempcode,“[ NextPage ]“)
’计算总页数并生成分页代码变量 htmlpage,并且格式如:全文共3页 [1] [2] [3]
Umax=Ubound(ptemp)
If Umax〉0 Then
For i=1 to Umax
htmname=“page“&i&“_“&rs(“htmname“)
htmlpage=htmlpage&“ 〈a href=’“&htmname&“’〉〈font color=’#D20000’〉〈b〉[“&i+1&“]〈/b〉〈/font〉〈/a〉“
Next
htmlpage=“〈a href=’“&rs(“htmname“)&“’〉〈font color=’#D20000’〉〈b〉[1]〈/b〉〈/font〉〈/a〉“&htmlpage
htmlpage=“ 全文共“&Umax+1&“页“ &htmlpage
end if
’写静态页
for j=0 to Umax
htmname=““ ’分页名称
ptempcode=““’分页文章部分
currentpage=““ ’目前页面为第几页
’开始在分页中加广告代码,如果文中有图片不加文内广告,否则在第50个字符后〈/p〉或〈br〉后加
ptemp(j)=trim(ptemp(j))
isimg=CheckTheChar(“〈IMG src“,ptemp(j)) ’判断文内是否有图片,用了CheckTheChar函数
isp=CheckTheChar(“〈/P〉“,ptemp(j))
if isimg=0 then ’文内无图片时加
if isp=0 then’文内没有〈/p〉
ptemp(j)=replace(ptemp(j),“〈BR〉“,“〈BR〉“&arad,50,1,1) ’只加一次,从第50个字符后的〈br〉开始
else ’文内有〈/p〉
ptemp(j)=replace(ptemp(j),“〈/P〉“,“〈/P〉“&arad,50,1,1) ’只加一次,从第50个之后后的〈p〉开始
end if
end if
’广告代码加完
’处理每个分页底部显示的分页代码和当前页等信息。
if Umax〉0 then
currentpage=“〈p align=’center’〉当前第“&j+1&“页“
end if
if j=0 then ’文章为一页,直接用文章原有的shtml名称
htmname=addfolder&rs(“htmname“)
else ’文章有多页,得出分页名称page1_原有名称
htmname=addfolder&“page“&j&“_“&rs(“htmname“)
end if
if j=Umax then ptemp(j)=ptemp(j)&“(全文完)“ ’如果为末页,自动加“(全文完)”
’ado要写的内容:模板页文章内容部分【$-acontent-$】用分页内容部分替换
ptempcode=replace(artempcode,“[$-acontent-$]“,ptemp(j)¤tpage&htmlpage)
’红色部分是文章内容最后的结果,加目前页面,分页
’打开obj写页面
Set objStream = Server.CreateObject(“ADODB.Stream“)
objStream.Open
objStream.Charset=“utf-8“
objStream.Position = 2
objStream.WriteText ptempcode
objStream.SaveToFile server.mappath(““&htmname&““),2
If Err.Number〈〉0 Then
Err.Clear
end if
set objStream=nothing
objStream.Close
next
response.write(“〈script〉alert(““成功生成静态网页!!““);〈/script〉“)
%〉
生成的页面中,菜单部分,广告部分以标准html代码存在,所以有利于搜索,广告商也没的说。