在內(nèi)容系統(tǒng)開發(fā)中,涉及內(nèi)容和形式分離的過程,也就是根據(jù)用戶自定義頁面模板然后替換成相關(guān)內(nèi)容的過程。這和外面很多整站的內(nèi)容管理系統(tǒng),有本質(zhì)上的區(qū)別。有不少內(nèi)容管理系統(tǒng),多少人用,都是一個樣子,因為頁面無法自定義,不懂編程的用戶無法修改。象那種,只填幾個參數(shù)就出來的網(wǎng)站,我估計是沒有什么前途的。因為人人都是一個樣子,人人都是會填那些參數(shù)的。
舉個例子,你查看一下以下幾個站點,你會認為他們是一套程序嗎?
www.blueidea.com
http://pages.blueidea.com
http://digi.blueidea.com
http://dsp.blueidea.com
http://www.dcshooter.com
如果我告訴你,他們都是一個程序,只是由相關(guān)的站長,設(shè)計不同的模板得到的頁面顯示,你就會發(fā)現(xiàn),這個系統(tǒng)的優(yōu)良性。
當(dāng)然由于這套系統(tǒng)的高端性,目前普通用戶無法使用,于是我開發(fā)了我自己的內(nèi)容管理系統(tǒng) kiss 內(nèi)容管理系統(tǒng)。
而要給用戶一個模板系統(tǒng),首先,就是要有一個簡單易懂的標(biāo)記系統(tǒng)。大家看看下面的代碼,看是否容易理解:
略有HTML經(jīng)驗的人,就知道,這是一個模板標(biāo)記里的循環(huán)標(biāo)記,因為這是最常用的,你看我們網(wǎng)站的首頁,列出10條文檔也就只需要寫一個這樣的標(biāo)記就完成了,這是不是讓不明白編程的人,也很容易做出自己設(shè)計的頁面出來呢?
參數(shù)說明:
channelid 為一個欄目的在數(shù)據(jù)庫中的ID
pagesize 為列舉多少個文檔
title 為標(biāo)題的長度
type 為列表列型,這里的”NEW”我們設(shè)定為最新的文檔
column 為顯示幾列
以上介紹是給不會編程,或者對不了解內(nèi)容系統(tǒng)的人做個普及,并且給我的內(nèi)容管理系統(tǒng)打個廣告,而且我想說的是,藍色理想站點用的內(nèi)容管理系統(tǒng)模板模塊,要比我的強大很多。
下面輪到程序員了,其它人可以不用往下看。
那么怎么把它們的值讀出來呢?
下面這個函數(shù)是最后的,用來解析所有模板的內(nèi)容
'【功能】自定義模板標(biāo)簽Function ProcessCustomTags(ByVal sContent) Dim objRegEx, Match, Matches '建立正則表達式 Set objRegEx = New RegExp '查找內(nèi)容 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) '循環(huán)已發(fā)現(xiàn)的匹配 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
在上面的代碼中,用到了正則表達式,如果你對它還不是很了解,請參閱相關(guān)資料,這里就不詳細介紹了。
那么怎么取出參數(shù)值呢,也是一個函數(shù):代碼拷貝框?
'【功能】取得模板標(biāo)簽的參數(shù)名'如:<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 '查找內(nèi)容 (the attribute name followed by double quotes etc) objRegEx.Pattern = lCase(strAttribute) & "=""[0-9a-zA-Z]*""" '忽略大小寫 objRegEx.IgnoreCase = True '全局查找 objRegEx.Global = True '執(zhí)行搜索 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:>內(nèi)容呢?
下面就是一個函數(shù):
'【功能】解析并替換相應(yīng)的模板標(biāo)簽內(nèi)容function ParseTag(ByVal strTag) dim arrResult, ClassName, arrAttributes, sTemp, i, objClass '如果標(biāo)簽是空的則退出函數(shù) 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是一個類,這里也不再詳說了。因為好久沒有說話了,不太習(xí)慣,呵呵。
結(jié)論,通過上面的函數(shù),你可以很快的編寫相關(guān)的模板程序了。希望對你有幫助。
新聞熱點
疑難解答