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
%>