ASP Crazy 模版操作類別最簡單的模板類別需要的朋友可以參考下。檔名:Awa_temp.Class.asp
複製代碼代碼如下:
<%
'Crazy蛙!模板操作類
'作者Crazy~蛙! QQ:379969387 歡迎交流
'版本V1.0;
Class AwaTemp
Public aa
Private FSO,StrTemp,FileData,GetDatas,StrHtmlName,htmlwrite,StrLabel,StrLValues
'===========================================
'建構子_初始化FSO元件
'===========================================
Private Sub Class_Initialize()
Set FSO=Server.CreateObject(Scripting.FileSystemObject)
End Sub
'===========================================
'構析函數_銷毀FSO元件
'===========================================
Private Sub Class_terminate()
Set FSO=nothing
End Sub
'===========================================
'類別屬性
'===========================================
'版本訊息
Public Property Get Version
Version=Crazy~蛙! 模板操作類別!V1.0版本;
End Property
'取得範本地址以及名稱
Public Property Let Temp(ByVal Values)
StrTemp=Values
End Property
'取得產生檔案的檔名
Public Property Let HtmlName(ByVal Values)
StrHtmlName=Values
End Property
'取得標籤
Public Property Let Label(ByVal Values)
StrLabel=Values
End Property
'取得將標籤替換的值
Public Property Let LValues(ByVal Values)
StrLValues=Values
End Property
'===========================================
'類別方法
'===========================================
'檢查模板設定以及是否存在
Private Function Check()
If StrTemp= Then
Check=<span style='color:red;'>錯誤:未設定模板檔案儲存位置! </span>
Else
If FSO.FileExists(StrTemp)=false Then
Check=<span style='color:red;'>錯誤:指定模板不存在! </span>
Else
Check=true
End If
End If
End Function
'讀取模板頁
Public Sub ReadTemp()
If Check()<>true Then
Response.Write Check()
Response.End()
Else
Set GetDatas=FSO.OpenTextFile(Server.MapPath(StrTemp))
FileData=GetDatas.ReadAll
GetDatas.Close
Set GetData=nothing
End If
End Sub
'替換內容
Public Function Rep()
If StrLabel= Then
Response.Write <span style='color:red;'>錯誤:未設定要替換的標籤! </span>
Response.End()
End If
If StrLValues= Then
Response.Write <span style='color:red;'>錯誤:未設定替換標籤的資料! </span>
Response.End()
End If
FileData=Replace(FileData,StrLabel,StrLValues)
End Function
'輸出
Public Sub Echo()
Response.Write FileData
End Sub
'產生靜態頁面
Public Sub GetHtml()
If StrHtmlName= Then
Response.Write <span style='color:red;'>錯誤:未設定產生Html檔案儲存位置以及檔案名稱! </span>
Response.End()
End If
Set htmlwrite=FSO.CreateTextFile(Server.MapPath(StrHtmlName),true)
'寫入網頁內容
htmlwrite.WriteLine FileData
htmlwrite.Close
set htmlwrite=Nothing
End Sub
End Class
%>
用法範例:
index.asp
複製代碼代碼如下:
<!--#include file=Awa_temp.Class.asp-->
<%
Set awa=New AwaTemp
With awa
.Temp=temp.tpl
.ReadTemp
.Label={tl1}
.LValues=蛋白
.Rep
.Echo
'.HtmlName=aa/index.html
'.GetHtml
End With
Set awa=nothing
%>
模版
temp.tpl:
複製代碼代碼如下:
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content=text/html; charset=gb2312 />
<title>無標題文件</title>
</head>
<body>
<table width=200 border=1>
<tr>
<td>{tl1}</td>
<td>{tl2}</td>
<td>{tl3}</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>