淺談 ASP 模板技術之參數傳遞

來源:互聯網
上載者:User

在內容系統開發中,涉及內容和形式分離的過程,也就是根據使用者自訂頁面模板然後替換成相關內容的過程。這和外面很多整站的內容管理系統,有本質上的區別。有不少內容管理系統,多少人用,都是一個樣子,因為頁面無法自訂,不懂編程的使用者無法修改。象那種,只填幾個參數就出來的網站,我估計是沒有什麼前途的。因為人人都是一個樣子,人人都是會填那些參數的。

舉個例子,你查看一下以下幾個網站,你會認為他們是一套程式嗎?
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" title="20" type="NEW" column="1">

略有HTML經驗的人,就知道,這是一個模板標記裡的迴圈標記,因為這是最常用的,你看我們網站的首頁,列出10條文檔也就只需要寫一個這樣的標記就完成了,這是不是讓不明白編程的人,也很容易做出自己設計的頁面出來呢?

參數說明:
channelid 為一個欄目的在資料庫中的ID
pagesize 為列舉多少個文檔
title 為標題的長度
type 為列表列型,這裡的”NEW”我們設定為最新的文檔
column 為顯示幾列

以上介紹是給不會編程,或者對不瞭解內容系統的人做個普及,並且給我的內容管理系統打個廣告,而且我想說的是,藍色理想網站用的內容管理系統模板模組,要比我的強大很多。

下面輪到程式員了,其它人可以不用往下看。
那麼怎麼把它們的值讀出來呢?
下面這個函數是最後的,用來解析所有模板的內容

複製代碼 代碼如下:'【功能】自訂模板標籤
Function ProcessCustomTags(ByVal sContent)
Dim objRegEx, Match, Matches
'建立Regex
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 = sContent
End Function

  在上面的代碼中,用到了Regex,如果你對它還不是很瞭解,請參閱相關資料,這裡就不詳細介紹了。

那麼怎麼取出參數值呢,也是一個函數:代碼拷貝框

複製代碼 代碼如下:'【功能】取得模板標籤的參數名
'如:<tag:loop channelid="1" pagesize="10" title="20" type="NEW" column="1">
function GetAttribute(ByVal strAttribute, ByVal strTag)
Dim objRegEx, Matches
'建立Regex
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 = 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.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 select
end function

上面的loop是一個類,這裡也不再詳說了。因為好久沒有說話了,不太習慣,呵呵。
  結論,通過上面的函數,你可以很快的編寫相關的模板程式了。希望對你有協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.