在內容系統開發中,涉及內容和形式分離的過程,也就是根據使用者自訂頁面模板然後替換成相關內容的過程。這和外面很多整站的內容管理系統,有本質上的差異。有不少內容管理系統,多少人用,都是一個樣子,因為頁面無法自訂,不懂程式設計的使用者無法修改。像那種,只填幾個參數就出來的網站,我估計是沒有什麼前途的。因為人人都是一個樣子,人人都是會填那些參數的。
舉個例子,你查看以下幾個站點,你會認為他們是一套程式嗎?
www.blueidea.com
http://pages.blueidea.com
http://digi.blueidea.com
http://dsp.blueidea.com
http://www.dcshooter.com
如果我告訴你,他們都是一個程序,只是由相關的站長,設計不同的模板得到的頁面顯示,你就會發現,這個系統的優良性。
當然由於這套系統的高階性,目前一般使用者無法使用,於是我開發了自己的內容管理系統kiss 內容管理系統。
而要給使用者一個模板系統,首先,就是要有一個簡單易懂的標記系統。大家看看下面的程式碼,看是否容易理解:
<tag:loop channelid=1 pagesize=10 />channelid 為一個欄目的在資料庫中的ID
pagesize 為列舉多少個文檔
title 為標題的長度
type 為列表列型,這裡的NEW我們設定為最新的文檔
column 為顯示幾列
以上介紹是給不會編程,或是對不了解內容系統的人做個普及,並且給我的內容管理系統打個廣告,而且我想說的是,藍色理想站點用的內容管理系統模板模組,要比我的強大很多。
下面輪到程式設計師了,它人可以不用往下看。
那麼要怎麼把它們的值讀出來呢?
下面這個函數是最後的,用來解析所有模板的內容
複製程式碼如下:'【功能】自訂範本標籤
Function ProcessCustomTags(ByVal sContent)
Dim objRegEx, Match, Matches
'建立正規表示式
Set objRegEx = New RegExp
'尋找內容
objRegEx.Pattern = <tag:.*/>
'忽略大小寫
objRegEx.IgnoreCase = True
'全域查找
objRegEx.Global = True
'Run the search against the content string we've been passed
Set Matches = objRegEx.Execute(sContent)
'循環已發現的匹配
For Each Match in Matches
'Replace each match with the appropriate HTML from our ParseTag function
sContent = Replace(sContent, Match. , ParseTag(Match.Value))
Next
'消毀物件
set Matches = nothing
set objRegEx = nothing
'傳回值
ProcessCustomTags = sContent
End Function
在上面的程式碼中,用到了正規表示式,如果你對它還不是很了解,請參閱相關資料,這裡就不詳細介紹了。
那麼要如何取出參數值呢,也是一個函數:代碼拷貝框
複製代碼代碼如下:'【功能】取得模板標籤的參數名
'如:<tag:loop channelid=1 pagesize=10 />function GetAttribute(ByVal strAttribute, ByVal strTag)
Dim objRegEx, Matches
'建立正規表示式
Set objRegEx = New RegExp
'找出內容(the attribute name followed by double quotes etc)
objRegEx.Pattern = lCase(strAttribute) & =[0-9a-zA-Z]*
'忽略大小寫
objRegEx.IgnoreCase = True
'全域查找
objRegEx.Global = True
'執行搜尋
MatSet Mat = objReg .Execute(strTag)
'如有符合的則回傳值, 不然回傳空值
if Matches.Count > 0 then
GetAttribute = Split(Matches(0).Value,)(1)
else
GetAttribute =
end if
'消毀物件
set Matches = nothing
set objRegEx = nothing
end function
OK了,那怎麼解析像上面<tagloop:>內容呢?
下面就是一個函數:
複製程式碼如下:'【功能】解析並取代對應的範本標籤內容
function ParseTag(ByVal strTag)
dim arrResult, ClassName, arrAttributes, sTemp, i, objClass
'如果標籤是空的則退出函數
if len(strTag) = 0 then exit function
'Split the match on the colon character (:)
arrResult = Split(strTag, :)
'Split the second item of the resulting array on the space character, to
'retrieve the name of the class
ClassName = Split(arrResult(1), )(0)
'Use a select case statement to work out which class we're dealing with
'and therefore which properties to populate etc
select case uCase(ClassName)
'It's a loop class, so instantiate one and get it's properties
case LOOP
set objClass = new LOOP_Class
LOOP.Channelid= GetAttribute(channelid, strTag)
LOOP.Paizesat Getbute(Patribute()dpages, strTag)
LOOP.Paizesat Getbute(pageb), struted (title, strTag)
LOOP.type = GetAttribute(Type, strTag)
ParseTag = LOOP.column (GetAttribute(column, strTag), true)
'Destroy our class object
set objClass = nothing
end select
end function
上面的這裡也不是一個類再詳說了。因為好久沒有說話了,不太習慣,呵呵。
結論,透過上面的函數,你可以很快的編寫相關的模板程式了。希望對你有幫助。