在內容系統開發中,涉及內容和形式分離的過程,也就是根據用戶自定義頁面模板然后替換成相關內容的過程。這和外面很多整站的內容管理系統,有本質上的區別。有不少內容管理系統,多少人用,都是一個樣子,因為頁面無法自定義,不懂編程的用戶無法修改。象那種,只填幾個參數就出來的網站,我估計是沒有什么前途的。因為人人都是一個樣子,人人都是會填那些參數的。
舉個例子,你查看一下以下幾個站點,你會認為他們是一套程序嗎?
www.blueidea.com
http://pages.blueidea.com
http://digi.blueidea.com
http://dsp.blueidea.com
http://www.dcshooter.com
如果我告訴你,他們都是一個程序,只是由相關的站長,設計不同的模板得到的頁面顯示,你就會發現,這個系統的優良性。
當然由于這套系統的高端性,目前普通用戶無法使用,于是我開發了我自己的內容管理系統 kiss 內容管理系統。
而要給用戶一個模板系統,首先,就是要有一個簡單易懂的標記系統。大家看看下面的代碼,看是否容易理解:
略有HTML經驗的人,就知道,這是一個模板標記里的循環標記,因為這是最常用的,你看我們網站的首頁,列出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.Value, ParseTag(Match.Value)) Next '消毀對象 set Matches = nothing set objRegEx = nothing '返回值 ProcessCustomTags = sContentEnd Function
在上面的代碼中,用到了正則表達式,如果你對它還不是很了解,請參閱相關資料,這里就不詳細介紹了。
那么怎么取出參數值呢,也是一個函數:代碼拷貝框?
'【功能】取得模板標簽的參數名'如:<tag:loop channelid="1" pagesize="10" title="20" type="NEW" column="1">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 '執行搜索 Set Matches = objRegEx.Execute(strTag) '如有匹配的則返回值, 不然返回空值 if Matches.Count > 0 then GetAttribute = Split(Matches(0).Value,"""")(1) else GetAttribute = "" end if '消毀對象 set Matches = nothing set objRegEx = nothingend 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.Pagesize= GetAttribute("pagesize", strTag") LOOP.title = GetAttribute("title", strTag") LOOP.type = GetAttribute("Type", strTag") ParseTag = LOOP.column (GetAttribute("column", strTag"), true) 'Destroy our class object set objClass = nothing end selectend function
上面的loop是一個類,這里也不再詳說了。因為好久沒有說話了,不太習慣,呵呵。
結論,通過上面的函數,你可以很快的編寫相關的模板程序了。希望對你有幫助。
新聞熱點
疑難解答