Asp模版类

来源:互联网 发布:淘宝网品牌女鞋kasscat 编辑:程序博客网 时间:2024/05/16 18:57

一个从类到实例都有的模板的例子
这个类还有很多不完善(比如效率),希望大家共同探讨,高手多多指正。
-----------------------------------------------------------------------
首先介绍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_unkno
wn()
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>

原创粉丝点击