电话:029-8161 4085
手机:189 9121 9418
3、使用例子
下面举三个例子进行说明。
1)简单的值替换
模板文件为myTemple.tpl,内容:
<html><title>ASP模板简单替换</title><body> 祝贺!你赢了一辆{some_color}法拉利! </body> |
下面是ASP代码(kktTemplate.inc.asp就是上面给出的模板类):
<!--#INCLUDE file="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 file="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> {plant}</td></tr> <!-- END plantList --> <!-- END animalList --> </table> </body> </html> |
ASP代码:
<!--#INCLUDE file="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> 小猪</td></tr> <tr><td> 小白</td></tr> <tr><td> 小强</td></tr> <tr><td>植物</td></tr> <tr><td> 玫瑰</td></tr> <tr><td> 向日葵</td></tr> </table> </body> </html> |
4、小结
本文主要介绍了基于ASP利用模板类实现代码与页面分离的方法,当然还有其它更好的解决方案。本文旨在抛砖引玉各位读者、WEB开发参与进来,多提宝贵意见,多作交流,共同进步!