,設計資料庫testmb.mdb
建立表moban:欄位m_id(自動編號,主關鍵字);欄位m_html(備忘類型)
2,假設第一模板內容代碼
將下列代碼拷貝到m_html欄位中
以下是程式碼片段:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop$</td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft$</td>
<td width="74%" bgcolor="#f3f3f3">$cnright$</td>
</tr>
</table>
</body>
</html>
注意$cntop$、$cnleft$、$cnright$,它們將要實現某些具體的程式功能
3,建立資料庫連接檔案conn.asp
以下是程式碼片段:
<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb")
conn.Open connstr
%>
4,建立特殊字元串轉換所需要的庫檔案lib.asp
該檔案的主要作用是將實現某些功能的ASP程式做成字程式,以方便調用。
以下是程式碼片段:
<%
dim topcode
sub cntop()
topcode="現在時間是:"
topcode=topcode&now()
end sub
dim leftcode,i
sub cnleft()
for i = 1 to 5
leftcode=leftcode&"<p>cnbruce.com"
next
end sub
dim rightcode
sub cnright()
for i = 1 to 9
rightcode=rightcode&"<hr color="&i&i&i&i&i&i&">"
next
end sub
%>
5,最後,調用資料庫中的模板代碼,將特殊字元串轉換。
以下是程式碼片段:
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%
sql="select * from moban where m_id=1"
set rs=Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
mb_code=rs("m_html")
rs.close
set rs=nothing
cntop()
mb_code=replace(mb_code,"$cntop$",topcode)
cnleft()
mb_code=replace(mb_code,"$cnleft$",leftcode)
cnright()
mb_code=replace(mb_code,"$cnright$",rightcode)
response.write mb_code
%>
該頁主要作用是將模板代碼進行顯示,並將其中的特殊代碼轉變為相對應子程式功能。
至此,ASP的模板功能基本完成,剩下的就是:建立具備編輯模板功能的程式頁面,將庫檔案改變為自己所需要程式功能……
2,2HTML技術又該如何?呢?如何使得ASP頁面轉變為HTML?一般都會想到FSO組件,因為該組件能建立任何檔案格式。
那麼其整個運行過程是怎麼樣的呢?
a,提供資訊輸入頁面進行資訊收集;
b,接受資訊值先儲存資料庫,再FSO組建檔案;
c,技術性完成任務,顯示剛被建立的HTML檔案的路徑地址。
該技術的實現過程中有如下幾個痛點:
i,FSO產生的檔案是直接放在一個大檔案夾下,還是單獨放在某個每日更新的子檔案夾中?可能表述不準確,這樣理解吧:相信通過FSO產生的檔案隨著時間的推移,檔案會越來越多,管理也會越來越亂……通常你可能看到一些地址諸如 http://www.xxx.com/a/2004-5-20/200405201111.html ; 可以分析得出應該是建立了當前日期的檔案夾。這樣,一天就是一個檔案夾的頁面內容,查看管理也就顯得比較合理。
ii,我在試圖通過以上方法建立檔案夾的時候,又發現了第二個問題。第一次通過FSO建立以當前日期命名的檔案夾,沒有問題。當我有新的檔案需要產生時,因為是同一個程式,所以,其又將會執行建立同樣的檔案夾。此時,FSO組件會發現該路徑已存在……卡殼-_-! 繼續處理,在首行添加代碼:
On Error Resume Next
嘿嘿,達到自欺欺人、掩耳盜鈴的效果。
iii,檔案夾是建立了,檔案該如何建立呢?主要也就是檔案名稱的產生。當然這個就需要自己來寫個函數,功能就是如何組建檔案名:)
<%
function makefilename(fname)
fname = fname ''前fname為變數,後fname為函數參數引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>
引用函數則:
<%fname = makefilename(now())%>
其實嘛,就是以年月日時分秒命名的檔案。
iv,最後,產生的檔案該如何查看到?當然需要把組建檔案的路徑儲存的資料庫中,並且添加到相對應的記錄集中了。當然,這在下面的資料庫設計時會提及到。
3,模板技術和2HTML技術的結合:將模板中特殊代碼的值替換為從表單接受過來的值,完成模板功能;將最終替換過的所有模板代碼產生HTML檔案。需要注意的是:替換應能將輸入資料的格式或者支援UBB的代碼徹底改變。
二,再進行資料庫設計
目前資料庫的設計需要兩個表:一個是存放模板資料的;一個是存放資訊內容的。
1,建立新資料庫asp2html.mdb
2,設計新資料庫表c_moban
欄位m_id(自動編號,主關鍵字);欄位m_html(備忘類型)。
並將下列完整的代碼拷貝至m_html欄位
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop$</td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft$</td>
<td width="74%" bgcolor="#f3f3f3">$cnright$</td>
</tr>
</table>
</body>
</html>
3,設計新資料庫表c_news
欄位c_id:自動編號,主關鍵字
欄位c_title:文本類型,儲存文章標題
欄位c_content:備忘類型,儲存文章內容
欄位c_filepath:文本類型,保持組建檔案的路徑地址
欄位c_time:日期/時間類型,預設值:Now()
三,頁面需求設計
1,首先建立一個存放HTML頁的檔案夾
在檔案同一目錄下,建立檔案夾newsfile,夾子內部主要存放產生的HTML頁面,當然內部還會採用程式方式建立以日期命名的子檔案夾,以方便瀏覽以及管理。
2,功能函數頁面lib.asp
<%
''組建檔案名的函數
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function
''保持資料格式不變的函數
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
3,資料庫連接頁面conn.asp
完成資料庫的字串串連方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>
4,資訊輸入頁面add.html
其實很簡單:)就是表單嘛。注意action是跳轉到addit.asp
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>
5,處理資料功能顯示頁面addit.asp
首先是處理接受過來的資料,並將值寫入資料庫;接著將模板代碼進行引用,並將其中特殊代碼轉換為接受值,最終通過FSO產生HTML頁面。其中需要注意的還有,組建檔案的路徑地址儲存至資料庫表。
<%''容錯處理
On Error Resume Next
%>
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%''接受傳遞值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
<%''產生HTML檔案名稱,建立檔案夾,指定檔案路徑
fname = makefilename(now()) ''makefilename為自訂函數
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
<%''將接受值及路徑保持至資料庫表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>
<%''開啟模板代碼,並將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%''產生HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="/showit.asp">瀏覽</a>
6,顯示資料庫表記錄,並做指向HTML頁的連結:showit.asp
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''開啟模板代碼,並將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%''產生HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
7,修改資料內容頁chang.asp
修改資料內容,同時也需要修改更新對應的HTML頁面。修改其實就是重建檔案,且檔案名稱和之前一樣,類似檔案的覆蓋。
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''開啟模板代碼,並將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop$",now())
mb_code=replace(mb_code,"$cnleft$",c_title)
mb_code=replace(mb_code,"$cnright$",c_content)
%>
<%''產生HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
8,刪除記錄頁del.asp
同樣!刪除,除了刪除資料庫表中的記錄,與其對應的HTML頁面也需刪除。代碼如下:
<!--#include file="conn.asp" -->
<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3
filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing
rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
<%response.redirect("showit.asp")%>
四,其它功能
範本管理員頁面:
不會每次都是開啟資料庫表進行增加或者修改模板代碼吧,所以,管理代碼的頁面程式不能少了,自己搗鼓下應該很簡單的。當然,之前管理員的登入認證程式就不在書中交代了:)還有,如果設計了多個模板,那麼在發表資訊的時候應添加模板選擇單選框,同樣在執行轉換HTML時,SQL選擇的不同m_id了。