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,多提宝贵意见,多作交流,共同进步!,共同进步!