首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 冲浪宝典-> 冲浪技巧
一个asp模板类-ASP教程,ASP应用
作者:网友供稿 点击:501
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
一个从类到实例都有的模板的例子
这个类还有很多不完善(比如效率),希望大家共同探讨,高手多多指正。
-----------------------------------------------------------------------
首先介绍asp中的类:
class对象,要求版本 5。
像一些面向对象的语言一样,asp中也提供类的支持,不过功能上不完善。
对于本类中使用的加以说明:
initialize 事件:在创建类的实例时发生此事件。

private sub class_initialize()
语句
end sub


此事件类似于c++中的构造函数,用来构造一个对象,本类中初始化了一些类的属性。
如何创建一个实例呢?

dim x
set x = new classname


此时x已经是经过class_initialize过程的一个对象了。
在类中定义的函数(过程),就叫做类的方法。
具体细节可参考:
mytju.com/classfiles/tju001/vbs5.5.chm
-----------------------------------------------------------------------
介绍模板技术:

大家写程序的时候(比如留言板),是不是都经历过循环+表格的方式呢?
这样写出的程序,既可读性差,又难于维护代码。
比如,以后要更改表格的颜色,或者改变页面布局(比如改变留言显示位置),
可能所有的代码都要重写(留言板当然不会,大的新闻系统就麻烦了)。
而模板技术的目的就是把:
表现层(页面)与逻辑层(代码)分开。
这是一个页面文件(就是模板):

下面是经过代码文件解析过的效果:

也许你会问,这又怎样呢?不是更麻烦了么?
也许在初期编写,会增加周期,可是,后期只要你把模板文件更换一下,就是一个
全新的结果了!而这个过程,asp的vbs脚本文件是完全不需修改的。
更进一步:上面的解析过程,是通过你编写的vbs来控制的,这样(理论上)就可以解析成
任何类型的文件,只要有模板!
这样你想到什么呢?对,新浪你也能作了!(夸张修辞)以前做新闻系统时,
news.asp?id=xxx(通过新闻id读取新闻)的时代就可以结束了,你可以生成静态页面:
2004090618.htm。
言归正传,说模板技术:
模板技术简单说就是把[模板文件]里面的待替换的变量(此处是用{}包含的内容),
替换成你所需要显示的内容。
普通的变量替换很容易:你只要了解fso的操作(广告秀:cnbruce有个asp教程,有详细讲解)
就不难,把模板文件的内容全部读取,然后对其中要替换的‘标记’进行替换(replace())就可以
实现效果。
而模板技术核心的东西应该是对于循环的处理:
以前看到的一些技术,是采用自定义一个标签,然后在标签中加入决定循环的
一些变量,比如名称、次数。
个人觉得这样可能违背了模板的初衷--表现层与逻辑层分离
而且正则的效率也不敢恭维。
对于循环的次数,应该完全由代码来控制,而不是要制作页面人员(美工)来添加。
所以我采用设置区块的思想:(借鉴自php)
把欲循环的页面代码,作为一个block,整个block再作为页面的一个变量来处理。

见下图:

需要做的就是加入一个html的注释(类似于上面说得标签)
而在代码部分:(tp为类的一个对象,set_block,set_bvar是类的一个方法)

对于循环的处理方式

tp.set_block "b" 设置区块,对应图中的 block b
for tmpc=1 to 10 设置循环次数,可以根据程序而定
tp.set_bvar "t1","alax"&tmpc 解析区块中的变量
tp.set_bvar "t2","proa"&tmpc
tp.set_bvar "t3","hotsjf"&tmpc
tp.prase_block(10-tmpc) 把区块作为变量来看,解析整个区块
next



需要实例的可以下载这个。
www.7csky.com/user/hotsjf/hottemplate.rar
运行test.asp即可看到采用 t.htm 为模板的效果。
效果演示点这:
代码文件:
202.4.136.222:802/test.asp
模板文件:
202.4.136.222:802/t.htm


之后的事情,就是对已经替换了全部标记的模板内容显示了。
显示可以直接将内容 response.write
也可以同fso,将内容写成文件,这时就可以生成其他类型的文件(例如.htm)。


谢谢大家多多提意见。

简单总结模板的特点:
表现层与逻辑层分开,页面人员不需考虑逻辑部分。
区块(block)的思想,解决asp中容易造成页面与代码交叉的部分。
带有缓存功能,加快文件生成速度。
==



模板代码:引用请保留信息,谢谢。
类:hottemplat.asp

<%
======================================
hottemplate
by hotsjf
2004.7.28
sjf008@tom.com
some rights reserved
======================================

on error resume next

class hottemplate
class member

dim file

dim var_key(50)
dim var_val(50)
dim var_num

dim bvar_key(50)
dim bvar_val(50)
dim bvar_num

dim block_name
dim block_content
dim new_block

dim debug
dim clear_unknown_tag

dim beginblockstart

dim beginblockend

dim endblockstart

dim endblockend

dim varbegin
dim varend


-------------------------------------------------------------------
初始化
private sub class_initialize
var_num=0
bvar_num=0
debug=false
beginblockstart="<!-- begin block "
beginblockend=" -->"
endblockstart="<!-- end block "
endblockend=" -->"
varbegin="{"
varend="}"
file=""
clear_unknown_tag=true
if debug then
response.write ("in class_initialize() <br>")
end if

end sub
-------------------------------------------------------------------
加载模板文件
public function loadfile(filename)
set fso = createobject("scripting.filesystemobject")
set f = fso.opentextfile( server.mappath(filename),1)
if err.number>0 then
exp_msg("no file matched!")
end if
file = f.readall
write into cache
application.lock
application("hot_tp"&filename)=file
application.unlock
if debug then
response.write ("in loadfile() ,filename:"&file&"<br>")
response.write ("in loadfile() ,cache:"&application("hot_tp"&filename)&"<br>")
end if
f.close
set fso=nothing
end function
-------------------------------------------------------------------
从缓存加载模板文件
public function loadfile_fromcache(filename)
if application("hot_tp"&filename)="" then
loadfile(filename)
else
file = application("hot_tp"&filename)
end if
end function
-------------------------------------------------------------------
更新缓存
public function update_cache(filename)
set fso = createobject("scripting.filesystemobject")
set f = fso.opentextfile( server.mappath(filename),1)
if err.number>0 then
exp_msg("no file matched!")
end if
file = f.readall
write into cache
application.lock
application("hot_tp"&filename)=file
application.unlock
end function


-------------------------------------------------------------------
设置全局模板变量
public function set_var(name,val)

if isarray(name) and isarray(val) then
tmp=0
for each key in name
if key="" then exit for
var_key(var_num)=key
var_val(var_num)=val(tmp)
tmp=tmp+1
if debug then
response.write ("in set_var() ,varname=>"&var_key(var_num)&"||val=>"&var_val

(var_num)&"||<br>")
end if
var_num=var_num+1
next
else
var_key(var_num)=name
var_val(var_num)=val
if debug then
response.write ("in set_var() ,varname=>"&var_key(var_num)&"||val=>"&var_val

(var_num)&"||<br>")
end if
var_num=var_num+1
end if
if err.number>0 then
exp_msg("set var error")
end if
end function
-------------------------------------------------------------------
设置块中模板变量
public function set_bvar(name,val)
if isarray(name) and isarray(val) then
tmp=0
for each key in name
if key="" then exit for
bvar_key(bvar_num)=key
bvar_val(bvar_num)=val(tmp)
tmp=tmp+1
if debug then
response.write ("in set_bvar() ,bvarname=>"&bvar_key(bvar_num)&"||bval=>"&bvar_val

(bvar_num)&"||<br>")
end if
bvar_num=bvar_num+1
next
else
bvar_key(bvar_num)=name
bvar_val(bvar_num)=val
if debug then
response.write ("in set_bvar() ,bvarname=>"&bvar_key(bvar_num)&"||bval=>"&bvar_val(bvar_num)

&"||<br>")
end if
bvar_num=bvar_num+1
end if
if err.number>0 then
exp_msg("set block var error")
end if
end function
-------------------------------------------------------------------
设置区块
public function set_block(name)
buf=file
start_block=beginblockstart & name & beginblockend
end_block=endblockstart & name & endblockend
start_block_len=len(start_block)
end_block_len=len(end_block)
start_block_pos=instr(file,start_block)
end_block_pos=instr(file,end_block)

if start_block_pos=0 or end_block_pos=0 then
exp_msg("block not found!")
end if
block_content=mid(file , start_block_pos + start_block_len , end_block_pos - start_block_pos -

start_block_len)

if debug then
response.write ("in set_block() ,blockame:"&name&"/val:"&block_content&"<br>")
end if
block_name=name
new_block=block_content
end function

-------------------------------------------------------------------
异常信息
private function exp_msg(msg)

response.write( "error accored in hottemplate :>"&msg)
err.clear()
response.end()

end function
-------------------------------------------------------------------
解析块内容
public function prase_block(append)

if append then
buf=block_content
else
buf=""
end if
i=0
for each k in bvar_key
if k="" then exit for
k=varbegin & k & varend
v=bvar_val(i)
new_block=replace(new_block , k , v)
i=i+1
next
bvar_num=0
new_block=new_block & buf
replace the block area in file handle
if append=0 then
file=replace(file , beginblockstart & block_name & beginblockend & block_content & endblockstart &

block_name & endblockend ,new_block)
end if
if err.number>0 then
exp_msg("prase block error!")
end if
if debug then
response.write "block:=>"&new_block&"<br>"
end if
end function

-------------------------------------------------------------------
解析文件内容
public function prase_file()
for vn=0 to var_num-1
k=varbegin & var_key(vn) & varend
v=var_val(vn)
file=replace(file , k , v)
if debug then
response.write ("in prase file () ,varname=>"&var_key(vn)&"|| val=>"&var_val(vn)&" || <br>")
end if
next
if err.number>0 then
exp_msg("prase file error")
end if
var_num=0
end function

-------------------------------------------------------------------
去掉未知模板变量
private function clear_unknown()
dim objregex, match, matches
set objregex = new regexp
objregex.pattern = varbegin&".*"&varend
objregex.global = true
set matches = objregex.execute(file)
for each match in matches
file=replace(file,match.value,"")
next
set matches = nothing
set objregex = nothing

end function

-------------------------------------------------------------------
直接输出
public function output()
if clear_unknown_tag then
clear_unknown()
end if
response.write file
end function

输出为文件
public function out_file(filename)
whichfile=server.mappath(filename)
set fso = createobject("scripting.filesystemobject")
set newfile = fso.createtextfile(whichfile,true)
newfile.write(file)
newfile.close
if clear_unknown_tag then
clear_unknown()
end if
end function


end class
%>


实例:
代码文件:test.asp

<!--#include file="hottemplat.asp"-->

<%
startime=timer()

dim tp
set tp=new hottemplate
dim tkey(2)
tkey(0)="t1"
tkey(1)="t2"
dim tval(2)
tval(0)="v1"
tval(1)="v2"


tp.loadfile "t.htm"
tp.loadfile_fromcache "t.htm"
tp.set_var "head","good"
tp.set_var "b2name","fkjdslajflksdakjlfjsdalkjflsadjkfljsad"
tp.set_var tkey,tval

tp.set_block "b"
for tmpc=1 to 10
tp.set_bvar "t1","alax"&tmpc
tp.set_bvar "t2","proa"&tmpc
tp.set_bvar "t3","hotsjf"&tmpc
tp.prase_block(10-tmpc)
next
tp.set_block "aa"
for tmpc=1 to 10
tp.set_bvar "bt1","haha"&tmpc
tp.set_bvar "bt2","hehe"&tmpc
tp.prase_block(10-tmpc)
next
tp.prase_file()
tp.output()
response.write("<hr><b>[executing time:&nbsp;"&formatnumber((timer()-startime)*1000,3)&"&nbsp;milliseconds.]</b><br>")
tp.out_file("tt.htm")
%>


模板(页面):t.htm

<style type="text/css">
<!--
.style1 {
color: #ff0000;
font-weight: bold;
}
-->
</style>
<span class="style1">{head}</span> is a hottemplate show!!
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>title1</td>
<td>title2</td>
<td>title3</td>
</tr>
<!-- begin block b -->
<tr>
<td>{t1}</td>
<td>{t2}</td>
<td>{t3}</td>
</tr>
<!-- end block b -->
</table>
<p><br>
here is anther block {b2name} </p>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bordercolor="#cccccc" bgcolor="#666666">
<tr bgcolor="#ffffff">
<td>bt1</td>
<td>bt2</td>
</tr>
<!-- begin block aa -->
<tr bgcolor="#ffffff">
<td>{bt1}</td>
<td>{bt2}</td>
</tr>
<!-- end block aa -->
</table>
<br>t1={t1}<br>t2={t2}<br>{tun}<br><br><br>
<p>&nbsp;</p>



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·不用SQL语句查询DataTable中的数据-.NET教程,数据库应用
·一个asp模板类-ASP教程,ASP应用
·谈有线电视系统前端设备调试
·表单提交“GET”的用法(form所有提交的值都会在地址栏出现)-ASP教程,ASP应用
·-48v整流器配置与直流系统设置
·c#下重写BUTTON控件-.NET教程,C#语言
·catv网络常见及疑难故障的分析处理
·常用路由协议的分析及比较
·mpls的体系结构与组网技术
·初级:.net框架下的MD5-.NET教程,.NET Framework
最新文章
·在线高速免费收看英超比赛的方法_冲浪技巧
·为什么成功的论坛和博客无法双管其下_站长心得
·做好一个好的网站策划_网络编辑
·windows live folders 新鲜试用_冲浪技巧
·什么样的网站放google adsense最理想_网赚技巧
·被google adsense 除名后怎么办_网赚技巧
·刘韧:站长站在草根与神话之间_站长访谈
·张弛有度的google adsense_google推广
·浏览器内嵌流媒体播放器silverlight_冲浪技巧
·高分辨率下界面布局的解决方案4_站长心得
相关主题
  • 一个asp快速字符串连接类_asp技巧
  • 一个asp快速字符串连接类-ASP教程,ASP技巧
  • 一个ASP.NET中使用的MessageBox类-.NET教程,Asp.Net开发
  • 一个ASP.NET的进度条-.NET教程,Asp.Net开发
  • 一个ASP.NET调试错误-.NET教程,Asp.Net开发
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭