欢迎光临
我们一直在努力

使用模板实现ASP代码与页面分离-ASP教程,ASP应用

建站超值云服务器,限时71元/月

使用模板实现asp代码与页面分离

  每个进行过较大型的asp-web应用程序设计的开发人员大概都有如下的经历:asp代码与页面html混淆难分,业务逻辑与显示方式绞合,使得代码难以理解、难以修改;程序编写必须在美工之后,成为项目瓶颈;整合的程序代码和html静态页面时,花费大量的时间才能得到理想的效果,兼作了美工。的确,用脚本语言开发web应用不容易将数据的处理和数据的显示分开,但在多人合作的情况下,如果无法将数据和显示分开,将大大影响开发的效率,专业分工的发挥。

  其它的脚本语言,如jsp、php都有自己的解决方案,asp的后一代产品asp.net也实现了代码与页面,似乎直接过渡到asp是不错的选择。但是总有这样或那样的原因让我们不能或暂时不能放弃asp直奔.net大营。从公司角度来看,转换语言是一笔不少的投资,包括雇佣熟手.net程序员、培训原有程序员、开发工具的转型、开发风格的转型、界面风格转变、接口风格、软件架构、文档、开发流程等等;这还意味着原有的代码必须在新语言环境里重写以实现最佳的效果和稳定性;同时将直接影响这段时间内项目的进度,更有可能导致个别程序员出走。由此看来在您决定转换语言之前,在原基础上寻求一种解决方案,才是最好的选择。

  php通过模板实现代码与页面,可供选择的有fasttemplate、phplib、smarty等多种,其中phplib的影响最大、使用最多。既然如此,我们直接把它搬到asp来,对于同时使用php和asp的公司还有很有好处:一、美工处理页面时,不管将要套用php还是asp,处理方式是一样,无须经过培训;二、程序员编写代码时,两种语言间的思路接近或一致,相同功能在两种语言实现时,只需拷贝过来略作修改即可,保证了工作效率和项目进度。

1、模板类的设计

  实现代码封装成为模板类,即是为了与phplib兼容,也使得代码方便管理与扩展。

  模板类要实现的目标为:从模板文件中读入显示的html代码,将这些显示代码中需要动态数据的地方替换为asp程序运算所得出的数据,然后按照一定的顺序输出。其中,替换的部分可以自由的设定。因此它必须完成如下任务:

·从模板文件中读取显示用的html代码。

·将模板文件和实际生成的数据结合,生成输出的结果。

·允许同时处理多个模板。

·允许模板的嵌套。

·允许对模板中的某个单独的部分进行处理。

实现方法:

采用fso读取模板文件

采用正则替换实现模板文件和数据的结合

处理多个模板用数组存储来实现。

模板的嵌套的实现主要的想法是:将模板和输出(任何中间的分析结果)一视同仁,都可拿来做替换,即可实现。

单独部分的处理的通过在模板文件中设定标注,然后在正则替换中结合标注来控制,实现部分替换。

2、模板类的实现

  给出具体代码之前,先把主要函数列出,用过phplib的朋友应该对此很熟悉了:

  1)public sub set_root(byval value) 设定模板默认目录

  2)public sub set_file(byval handle,byval filename) 读取文件

  3)public sub set_var(byval name, byval value, byval append) 设置映射数据-替换变量

  4)public sub unset_var(byval name) 取消数据映射

  5)public sub set_block(byval parent, byval blocktag, byval name) 设置数据块

  6)public sub set_unknowns(byval unknowns) 设定未指定映射的标记处理方式

  7)public sub parse(byval name, byval blocktag, byval append) 执行模板文件与数据的结合

  8)public sub p(byval name) 输出处理结果

实现代码:

<%

=======================================================================

class name: kkttemplate asp页面模板对象

design by : 彭国辉

date: 2004-07-05

website: http://kacarton.yeah.net/

email: kacarton@sohu.com

本对象中使用了set_var、set_block等命名方法是为了兼容phplib

=======================================================================

class kkttemplate

private m_filename, m_root, m_unknowns, m_lasterror, m_haltonerr

private m_valuelist, m_blocklist

private m_regexp

构造函数

private sub class_initialize

set m_valuelist = createobject("scripting.dictionary")

set m_blocklist = createobject("scripting.dictionary")

set m_regexp = new regexp

m_regexp.ignorecase = true

m_regexp.global = true

m_filename = ""

m_root = ""

m_unknowns = "remove"

m_lasterror = ""

m_haltonerr = true

end sub

析构函数

private sub class_terminate

set m_regexp = nothing

set m_blockmatches = nothing

set m_valuematches = nothing

end sub

public property get classname()

classname = "kkttemplate"

end property

public property get version()

version = "1.0"

end property

public sub about()

response.write("kkttemplate asp页面模板类<br>" & vbcrlf &_

"程序设计:彭国辉 2004-07-05<br>" & vbcrlf &_

"个人网站:<a href=http://kacarton.yeah.net>http://kacarton.yeah.net</a><br>" & vbcrlf &_

"电子邮件:<a href=mailto:kacarton@sohu.com>kacarton@sohu.com</a><br>")

end sub

检查目录是否存在

public function folderexist(byval path)

dim fso

set fso = createobject("scripting.filesystemobject")

folderexist = fso.folderexists(server.mappath(path))

set fso = nothing

end function

读取文件内容

private function loadfile()

dim filename, fso, hndfile

filename = m_root

if right(filename, 1)<>"/" and right(filename, 1)<>"\" then filename = filename & "/"

filename = server.mappath(filename & m_filename)

set fso = createobject("scripting.filesystemobject")

if not fso.fileexists(filename) then showerror("模板文件" & m_filename & "不存在!")

set hndfile = fso.opentextfile(filename)

loadfile = hndfile.readall

set hndfile = nothing

set fso = nothing

if loadfile = "" then showerror("不能读取模板文件" & m_filename & "或文件为空!")

end function

处理错误信息

private sub showerror(byval msg)

m_lasterror = msg

response.write "<font color=red style=font-size;14px><b>模板错误:" & msg & "</b></font><br>"

if m_haltonerr then response.end

end sub

设置模板文件默认目录

ex: kkttemplate.set_root("/tmplate")

kkttemplate.root = "/tmplate"

root = kkttemplate.get_root()

root = kkttemplate.root

使用类似set_root这样的命名方法是为了兼容phplib,以下将不再重复说明

public sub set_root(byval value)

if not folderexist(value) then showerror(value & "不是有效目录或目录不存在!")

m_root = value

end sub

public function get_root()

get_root = m_root

end function

public property let root(byval value)

set_root(value)

end property

public property get root()

root = m_root

end property

设置模板文件

ex: kkttemplate.set_file("hndtpl", "index.htm")

本类不支持多模板文件,handle为兼容phplib而保留

public sub set_file(byval handle,byval filename)

m_filename = filename

m_blocklist.add handle, loadfile()

end sub

public function get_file()

get_file = m_filename

end function

public property let file(handle, filename)

set_file handle, filename

end property

public property get file()

file = m_filename

end property

设置对未指定的标记的处理方式,有keep、remove、comment三种

public sub set_unknowns(byval unknowns)

m_unknowns = unknowns

end sub

public function get_unknowns()

get_unknowns = m_unknowns

end function

public property let unknowns(byval unknown)

m_unknowns = unknown

end property

public property get unknowns()

unknowns = m_unknowns

end property

public sub set_block(byval parent, byval blocktag, byval name)

dim matches

m_regexp.pattern = "<!–\s+begin " & blocktag & "\s+–>([\s\s.]*)<!–\s+end " & blocktag & "\s+–>"

if not m_blocklist.exists(parent) then showerror("未指定的块标记" & parent)

set matches = m_regexp.execute(m_blocklist.item(parent))

for each match in matches

m_blocklist.add blocktag, match.submatches(0)

m_blocklist.item(parent) = replace(m_blocklist.item(parent), match.value, "{" & name & "}")

next

set matches = nothing

end sub

public sub set_var(byval name, byval value, byval append)

dim val

if isnull(value) then val = "" else val = value

if m_valuelist.exists(name) then

if append then m_valuelist.item(name) = m_valuelist.item(name) & val _

else m_valuelist.item(name) = val

else

m_valuelist.add name, value

end if

end sub

public sub unset_var(byval name)

if m_valuelist.exists(name) then m_valuelist.remove(name)

end sub

private function instancevalue(byval blocktag)

dim keys, i

instancevalue = m_blocklist.item(blocktag)

keys = m_valuelist.keys

for i=0 to m_valuelist.count-1

instancevalue = replace(instancevalue, "{" & keys(i) & "}", m_valuelist.item(keys(i)))

next

end function

public sub parse(byval name, byval blocktag, byval append)

if not m_blocklist.exists(blocktag) then showerror("未指定的 块标记" & parent)

if m_valuelist.exists(name) then

if append then m_valuelist.item(name) = m_valuelist.item(name) & instancevalue(blocktag) _

else m_valuelist.item(name) = instancevalue(blocktag)

else

m_valuelist.add name, instancevalue(blocktag)

end if

end sub

private function finish(byval content)

select case m_unknowns

case "keep" finish = content

case "remove"

m_regexp.pattern = "\{[^ \t\r\n}]+\}"

finish = m_regexp.replace(content, "")

case "comment"

m_regexp.pattern = "\{([^ \t\r\n}]+)\}"

finish = m_regexp.replace(content, "<!– template variable $1 undefined –>")

case else finish = content

end select

end function

public sub p(byval name)

if not m_valuelist.exists(name) then showerror("不存在的标记" & name)

response.write(finish(m_valuelist.item(name)))

end sub

end class

%>

3、使用例子

  下面举三个例子进行说明。

1)简单的值替换

模板文件为mytemple.tpl,内容:

<html><title>asp模板简单替换</title><body>

祝贺!你赢了一辆{some_color}法拉利!

</body>

下面是asp代码(kkttemplate.inc.asp就是上面给出的模板类):

<!–#include virtual="kkttemplate.inc.asp"–>

<%

dim my_color, kkt

my_color = "红色的"

set kkt = new kkttemplate 创建模板对象

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" 输出out的内容

set kkt = nothing 销毁模板对象

%>

执行后输出为:

<html><title>asp模板简单替换</title><body>

祝贺!你赢了一辆红色的法拉利!

</body>

2)循环块演示例子

模板文件mytemple2.tpl:

<html><title>asp模板-块的演示</title><body>

<table cellspacing="2" border="1"><tr><td>下面的动物您喜欢哪一种</td></tr>

<!– begin animallist –>

<tr><td><input type="radio" name="chk">{animal}</td></tr>

<!– end animallist –>

</table>

</body>

asp代码:

<!–#include virtual="kkttemplate.inc.asp"–>

<%

dim animal, kkt, i

animal = array("小猪","小狗","小强")

set kkt = new kkttemplate

kkt.set_file "hndkkttemp", "mytemple2.tpl"

kkt.set_block "hndkkttemp", "animallist", "list"

for i=0 to ubound(animal)

kkt.set_var "animal", animal(i), false

kkt.parse "list", "animallist", true

next

kkt.parse "out", "hndkkttemp", false

kkt.p "out"

set kkt = nothing

%>

执行结果:

<html><title>asp模板-块的演示</title><body>

<table cellspacing="2" border="1"><tr><td>下面的动物您喜欢哪一种</td></tr>

<tr><td><input type="radio" name="chk">小猪</td></tr>

<tr><td><input type="radio" name="chk">小狗</td></tr>

<tr><td><input type="radio" name="chk">小强</td></tr>

</table>

</body>

3)嵌套块演示

模板文件mytemple3.tpl:

<html><title>asp模板-嵌套块演示</title>

<body><table width="400" border="1" bordercolor="#000000">

<tr><td><div align="center">{myname}测试</div></td></tr>

<tr><td>我的动植物园:</td> </tr>

<!– begin animallist –>

<tr><td>{animal}</td></tr>

<!– begin plantlist –>

<tr><td>&nbsp;&nbsp;{plant}</td></tr>

<!– end plantlist –>

<!– end animallist –>

</table>

</body>

</html>

asp代码:

<!–#include virtual="kkttemplate.inc.asp"–>

<%

dim my_color, kkt, myname, animal, plant

set kkt = new kkttemplate

myname = "kkttemplate block test…"

animal = array("动物", "植物")

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", "plantlist", "p"

for i=0 to ubound(animal)

kkt.set_var "animal", animal(i), false

kkt.unset_var "p"

kkt.set_var "p", "", false

for j=0 to ubound(plant(i))

kkt.set_var "plant", plant(i)(j), false

kkt.parse "p", "plantlist", true

next

kkt.parse "a", "animallist", true

next

kkt.parse "out", "hndkkttemp", false

kkt.p "out"

%>

执行结果:

<html><title>asp模板-嵌套块演示</title>

<body><table width="400" border="1" bordercolor="#000000">

<tr><td><div align="center">kkttemplate block test…测试</div></td></tr>

<tr><td>我的动植物园:</td> </tr>

<tr><td>动物</td></tr>

<tr><td>&nbsp;&nbsp;小猪</td></tr>

<tr><td>&nbsp;&nbsp;小白</td></tr>

<tr><td>&nbsp;&nbsp;小强</td></tr>

<tr><td>植物</td></tr>

<tr><td>&nbsp;&nbsp;玫瑰</td></tr>

<tr><td>&nbsp;&nbsp;向日葵</td></tr>

</table>

</body>

</html>

本文提及的所有代码可从此处下载:http://www.freewebs.com/kacarton/web/kkttemplate.rar(3.53k)

4、小结

  本文主要介绍了基于asp利用模板类实现代码与页面分离的方法,当然还有其它更好的解决方案。本文旨在抛砖引玉各位读者、web开发参与进来,多提宝贵意见,多作交流,共同进步!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用模板实现ASP代码与页面分离-ASP教程,ASP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章