YPE HTML公共” - // W3C // DTD HTML 4.0 Transitional // en”“” http://www.w3.org/tr/rec-html40/loose.dtd“>
YPE HTML公共” - // W3C // DTD HTML 4.0 Transitional // en”“” http://www.w3.org/tr/rec-html40/loose.dtd“>
今天愛站小編就為大家帶來一篇asp代碼與頁面的分離技術,小編覺得挺不錯的,現在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧。 ,一起跟隨小編過來看看吧。
每個進行過較大型的asp-web:asp代碼與頁面html混淆難分,業務邏輯與顯示方式絞合
其它的腳本語言,如jsp php都有自己的解決方案,asp的後一代產品asp.net也實現了代碼與頁面,a asp asp ar asp a asp a asp是不錯的選擇。但是總有這樣或那樣的原因讓我們不能或暫時不能放棄.net。
php通過模板實現代碼與頁面,可供選擇的有快速板,phplib smarty等多種,其中phplib 的影響最大、使用最多。既然如此,a asp來,對於同時使用php php phpphp和Asp的公司還有很有好處:一、美工處理頁面時:一、美工處理頁面時
1 、模板類的設計
實現代碼封裝成為模板類,即是為了與phplib兼容,也使得代碼方便管理與擴展。 ,也使得代碼方便管理與擴展。
模板類要實現的目標為:從模板文件中讀入顯示的html代碼,將這些顯示代碼中需要動態數據的地方替換為asp程序運算所得出的數據,然後按照一定的順序輸出。其中,替換的部分可以自由的設定。因此它必須完成如下任務::
·從模板文件中讀取顯示用的html代碼。
·將模板文件和實際生成的數據結合,生成輸出的結果。
·允許同時處理多個模板。
·允許模板的嵌套。
·允許對模板中的某個單獨的部分進行處理。
實現方法:
採用fso讀取模板文件
採用正則替換實現模板文件和數據的結合
處理多個模板用數組存儲來實現。
模板的嵌套的實現主要的想法是:將模板和輸出(任何中間的分析結果)一視同仁,都可拿來做替換,即可實現。 ,即可實現。
單獨部分的處理的通過在模板文件中設定標註,然後在正則替換中結合標註來控制,實現部分替換。 ,實現部分替換。
2 、模板類的實現
給出具體代碼之前,先把主要函數列出,用過phplib::
1)publicsubset_root(byvalvalue)設定模板默認目錄
2)PublicSubset_file(byvalhandle,byvalfilename)讀取文件
3)PublicSubset_var(byvalname,byvalvalue,byvalappend)設置映射數據-替換變量
4)publicsubunset_var(byvalname)取消數據映射
5)PublicSubset_block(byvalparent,byvalblocktag,byvalname)設置數據塊
6)PublicSubset_unknowns(byvalunknowns)設定未指定映射的標記處理方式
7)PublicSubparse(byvalname,byvalblocktag,byvalappend)執行模板文件與數據的結合
8)PublicSubp(byvalname)輸出處理結果
實現代碼:
'=============================================================================
'className:kkttemplateasp頁面模闆對象
'designby:彭國輝
'日期:2004-07-05
'網站:http://kacarton.yeah.net/
'電子郵件:[email protected]
'
'本對像中使用了set_var,set_block等命名方法是為了兼容phplib
'=============================================================================
ClassKktTemplate
privatem_filename,m_root,m_unknowns,m_lasterror,m_haltonerr
privatem_valuelist,m_blocklist
privatem_regexp
'構造函數
PrivatesubClass_Initialize
setm_valuelist = createObject(“ scripting.dictionary”)
setm_blocklist = createObject(“ scripting.dictionary”)
setm_regexp = newregexp
m_regexp.ignorecase = true
m_regexp.global = true
m_filename =“”
m_root =“”
m_unknowns =“刪除”
m_lasterror =“”
m_haltonerr = true
endub
'析構函數
PrivatesUbClass_terminate
setm_regexp =什麼都沒有
setm_blockMatches =什麼都沒有
setm_valueMatches = nothing
endub
publicPropertygetClassName()
className =“ kkttemplate”
Endproperty
publicPropertygetVersion()
版本=“ 1.0”
Endproperty
publicSubObout()
response.write(“ kkttemplateasp頁面模板類
”&vbcrlf&_
“:彭國輝:彭國輝2004-07-05
”&vbcrlf&_
“:http://kacarton.yeah.net
”&vbcrlf&_
“電子郵件:[email protected]
”)
endub
'檢查目錄是否存在
publicfunctionfolderexist(byvalpath)
Dimfso
setfso = createObject(“ scripting.filesystemobject”)
folderexist = fso.folderexists(server.mappath(path))
setfso =沒有
最終功能
'讀取文件內容
privateFunctionloadfile()
DimFilename,FSO,Hndfile
文件名= m_root
ifright(filename,1)“/” andright(filename,1)“/” thenFileName = fileName&“/”/“”
filename = server.mappath(文件名和m_filename)
setfso = createObject(“ scripting.filesystemobject”)
ifnotfso.fileexists(文件名)
sethndfile = fso.opentextfile(文件名)
loadFile = hndfile.readall
sethndfile =沒有
setfso =沒有
ifloadfile =“” thenshowerror(“不能讀取模板文件”&m_filename&“或文件為空!”)
最終功能
'處理錯誤信息
PrivatesUbshowerRor(Byvalmsg)
m_lasterror = msg
wrespy.write“模板錯誤:”&msg&“
“
ifm_haltonerthenresponse.end
endub
'設置模板文件默認目錄
'ex:kkttemplate.set_root(“/tmplate”)
'kkttemplate.root =“/tmplate”
'root = kkttemplate.get_root()
'root = kkttemplate.root
'使用類似set_root這樣的命名方法是為了兼容phplib,以下將不再重複說明
PublicSubset_root(byvalvalue)
ifnotfolderexist(value)thenshowerror(value&'不是有效目錄或目錄不存在!”)
m_root =值
endub
publicFunctionget_root()
get_root = m_root
最終功能
publicPropertyletlet(字節)
set_root(值)
Endproperty
publicPropertygetRoot()
root = m_root
Endproperty
'設置模板文件
'ex:kkttemplate.set_file(“ hndtpl”,“ index.htm”)
'本類不支持多模板文件,處理為兼容phplib而保留
PublicSubset_file(byvalhandle,byvalfilename)
m_filename =文件名
m_blocklist.addhandle,loadfile()
endub
publicFunction_file()
get_file = m_filename
最終功能
'publicPropertyletFile(句柄,文件名)
'set_filehandle,文件名
'endproperty
'publicPropertygetFile()
'file = m_filename
'endproperty
'設置對未指定的標記的處理方式,有保持,刪除,註釋三種
PublicSubset_unknowns(byvalunknowns)
m_unknowns =未知
endub
publicFunctionget_unknowns()
get_unknowns = m_unknowns
最終功能
publicProperpyletunknowns(byvalunknown)
m_unknowns =未知
Endproperty
publicPropertygetUnknowns()
未知= m_unknowns
Endproperty
PublicSubset_block(byvalparent,byvalblocktag,byvalname)
DimMatches
m_regexp.pattern =“ ([/s/s.] “
ifnotm_blocklist.exists(parent)thenshowerror(“未指定的塊標記”&parent)
setMatches = m_regexp.execute(m_blocklist.item(parent))
foreachMatchinMatches
m_blocklist.addblocktag,match.submatches(0)
m_blocklist.item(parent)= replace(m_blocklist.item(parent),match.value,“ {”&name&“&”}”)
下一個
setMatches =沒有
endub
PublicSubset_var(byvalname,byvalvalue,byvalappend)
Dimval
ifisnull(value)thenval =“”其他val = value
ifm_valuelist.exists(name)
ifappendthenm_valuelist.item(name)= m_valuelist.item(name)&val_
elsem_valuelist.item(name)= val
別的
m_valuelist.addname,值
Endif
endub
PublicSubunset_var(byvalname)
ifm_valuelist.exists(name)thenm_valuelist.remove(name)
endub
私有功能instanceValue(byvalblocktag)
Dimkeys,我
instanceValue = m_blocklist.item(blocktag)
鍵= m_valuelist.keys
fori = 0TOM_VALUELIST.COUNT-1
instanceValue = replace(instanceValue,“ {”&keys(i)&“}”,m_valuelist.item(keys(i)))
下一個
最終功能
PublicSubparse(byvalname,byvalblocktag,byvalappend)
ifnotm_blocklist.exists(blocktag)thenshowerror(“未指定的塊標記”和parent)
ifm_valuelist.exists(name)
ifappendthenm_valuelist.item(name)= m_valuelist.item(name)&instanceValue(blocktag)_
elsem_valuelist.item(name)= instanceValue(blockTag)
別的
m_valuelist.addname,instanceValue(blockTag)
Endif
endub
私人finctionfinish(byvalcontent)
selectcasem_unknowns
案例“保持”完成=內容
案例“刪除”
m_regexp.pattern =“/{[^/t/r/n}]+/}”
完成= m_regexp.replace(內容,“”)
案例“評論”
m_regexp.pattern =“/{([^/t/t/r/n}]+)+)/}”
完成= m_regexp.replace(內容,” ”)
caseelsefinish = content
endSelect
最終功能
PublicSubp(byvalname)
ifnotm_valuelist.exists(name)thenshowerror(“不存在的標記”&name)
wress.write(完成(m_valuelist.item(name)))
endub
末期級
%>
3 、使用例子
下面舉三個例子進行說明。
1)簡單的值替換
模板文件為mytemple.tpl,內容:
祝賀!你贏了一輛 {some_color} 法拉利!
下面是asp 代碼( kkttemplate.inc.asp就是上面給出的模板類):
dimmy_color,kkt
my_color =“紅色的”
setkkt = newkkttemplate'創建模闆對象
kkt.set_file“ hndkkttemp”,“ mytemple.tpl”'設置並讀取模板文件mytemple.tpl
kkt.set_var“ some_color”,my_color,false'設置模板變量some_color = my_color的值
kkt.parse“ out”,“ hndkkttemp”,false'模板變量out =處理後的文件
kkt.p“ out”''輸出的內容
setkkt = nothing'銷毀模闆對象
%>
執行後輸出為:
祝賀!你贏了一輛紅色的法拉利!
2)循環塊演示例子
模板文件mytemple2.tpl:
| 下面的動物您喜歡哪一種 |
| {動物} |
ASP代碼:
Dimanimal,KKT,i
動物=陣列(“小豬”,“小狗”,“小強”)
setkkt = newkkttemplate
kkt.set_file“ hndkkttemp”,“ mytemple2.tpl”
kkt.set_block“ hndkkttemp”,“ animallist”,“ list”
fori = 0toubound(動物)
kkt.set_var“動物”,動物(i),假
KKT.PARSE“列表”,“ Animallist”,是的
下一個
kkt.parse“ out”,“ hndkkttemp”,false
kkt.p“ out”
setkkt =沒有
%>
執行結果:
| 下面的動物您喜歡哪一種 |
| 小豬 |
| 小狗 |
| 小強 |
3)嵌套塊演示
模板文件mytemple3.tpl:
| {myname}測試 |
| 我的動植物園: |
| {動物} |
| {植物} |
ASP代碼:
dimmy_color,kkt,myname,動物,植物
setkkt = newkkttemplate
myname =“ kkttemplateblocktest ...”
動物=陣列(“動物”,“植物”)
plant = array(array(“小豬”,“小白”,“小強”),array(“玫瑰”,“向日葵”))
kkt.set_file“ hndkkttemp”,“ mytemple3.tpl”
kkt.set_var“ myname”,myname,false
kkt.set_block“ hndkkttemp”,“ animallist”,“ a”
kkt.set_block“ Animallist”,“植物學”,“ P”
fori = 0toubound(動物)
kkt.set_var“動物”,動物(i),假
kkt.unset_var“ p”
'kkt.set_var“ p”,“”,false
forj = 0toubound(植物(i))
kkt.set_var“植物”,植物(i)(j),false
Kkt.Parse“ P”,“植物學”,是的
下一個
KKT.PARSE“ A”,“ Animallist”,是的
下一個
kkt.parse“ out”,“ hndkkttemp”,false
kkt.p“ out”
%>
執行結果:
| kkttemplateblocktest ...測試 |
| 我的動植物園: |
| 動物 |
| 小豬 |
| 小白 |
| 小強 |
| 植物 |
| 玫瑰 |
| 向日葵 |
4 、小結
本文主要介紹了基於asp利用模板類實現代碼與頁面分離的方法,當然還有其它更好的解決方案。本文旨在拋磚引玉各位讀者、 web,多提寶貴意見,多作交流,共同進步! ,共同進步!