ASP 根据模板生成HTML静态文件类
<%
'使用范例:
' dim tpl
' set tpl = New template
' tpl.setTplPath = "../template/footer.tpl"
' tpl.setTags = array("<!--{标签名称}-->")
' tpl.setNotes = array("替换内容")
' tpl.setStoreF = "../html/" '使用自动生成文件名时请在未尾加"/"
' tpl.exeTpl
' Set tpl = Nothing
'----------------------------------------------------------
Class template
Private objFso
Private strFileCode ' 读取模板文件的内容
Private intRnd ' 生成随机数,组合成新文件名
Private tplpath ' 模板名称
Private StorePath ' 生成文件存放目录
Private StoreFname ' 生成文件名称
Private arrTags ' 标签数组
Private arrNotes ' 内容
' 在使用 Set 创建对象时自动执行
Private Sub Class_Initialize()
set objFso = CreateObject("scripting.filesystemobject")
End Sub
' 模板文件位置+名称 只写
Public Property Let setTplPath(ByVal tName)
tplpath = Server.MapPath(Replace(tName,"//","/"))
End Property
' 存储文件位置+名称 只写
Public Property Let setStoreF(ByVal tName)
tName = Replace(tName,"//","/")
' 取文件名
dim arrY
arrY = split(tName,"/")
StoreFname = Ubound(arrY)
StoreFname = arrY(StoreFname)
' 取目录名
StorePath = Replace(tName,StoreFname,"")
StorePath = Server.MapPath(StorePath)
End Property
' 标签 只写
Public Property Let setTags(ByVal strString)
arrTags = strString
End Property
' 内容 只写
Public Property Let setNotes(ByVal strString)
arrNotes = strString
End Property
' 生成在Min 与 Max之间取随机数
Private Sub rnd_Integer(Min,Max)
Randomize
intRnd = CInt((Max-Min+1)*Rnd()+Min)
End Sub
' 生成文件名:时间+随机数
Private Sub makeFilename
dim fname
fname = now()
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
call rnd_Integer(0,9999)
StoreFname = fname&intRnd&".html"
fname = null
End Sub
' 读取模板文件内容
Private Sub LoadTemplate
If objFso.FileExists(tplpath) Then
Set FileObj = objFso.GetFile(tplpath)
Set FileStreamObj = FileObj.OpenAsTextStream(1)
If Not FileStreamObj.AtEndOfStream Then
strFileCode = FileStreamObj.ReadAll
Else
Message 1002
End If
Else
Message 1001
End If
Set FileObj = Nothing:Set FileStreamObj = Nothing
End Sub
' 更新页面内容
Private Sub updateNotes
if IsArray(arrNotes) and IsArray(arrTags) then
if Ubound(arrNotes) = Ubound(arrTags) then
LoadTemplate
For intI = Lbound(arrNotes) to Ubound(arrNotes)
strFileCode = Replace(strFileCode,arrTags(intI),arrNotes(intI))
Next
else
Message 1004
end if
else
Message 1004
end if
End Sub
' 创建文件夹
Private Sub makeFold(ByVal strFoladName)
on error resume next
'判断文件夹是否存在,(about files:objFso.FileExists)
If false = objFso.FolderExists(strFoladName) Then
objFso.CreateFolder(strFoladName)
if err>0 then
err.clear
Message 1003
end if
End If
End Sub
' 创建文件
Private Sub makeFile
dim arrX,intI,strW
strW = ""
arrX = split(StorePath,"/")
' 判断目录是否存在,决定是否创建目录
For intI = Lbound(arrX) to Ubound(arrX)
makeFold strW&arrX(intI)
strW = strW&arrX(intI)
Next
' 未设置文件名则使用日期加随机数作为文件名
if StoreFname = "" then
makeFilename
end if
' 更新内容
updateNotes
' 生成文件 object.CreateTextFile(filename[, overwrite[, unicode]])
Set objFout = objFso.CreateTextFile(StorePath&"/"&StoreFname,true)
' 输入内容
objFout.WriteLine strFileCode
objFout.close
set objFso = Nothing
' 提示操作成功
Message 1000
End Sub
' 执行
public Sub exeTpl
makeFile
End Sub
' 返回
Private Sub Message(ByVal s)
Select Case s
Case 1000
Response.Write "操作成功!"&StoreFname
Case 1001
Response.Write "模板不存在,请先绑定!"
Case 1002
Response.Write "模板内容为空!"
Case 1003
Response.Write "文件目录创建失败!"
Case Else
End Select
End Sub
'在使用 Set 释放对象时自动执行
Private Sub Class_Terminate
Set objFso = Nothing
End Sub
End Class
%>