呵呵,其实这个写了都挺久的了,看看里面的日期就知道了,只不过一直不想发布出来,这次在做一个小型的论坛,就把原来自己的的那个复杂的摸板引擎修改了一翻拿来用,因为把ASP代码和HTML代码分离了开来,所以论坛是可以变换风格,在用这个摸板引擎的过程中发现了它功能上还是有点不足,就是在循环标签替换上,不过如果结合数组可以轻松实现的,呵呵,具体用法暂且不说了...等有时间再把教程写出来
<Script Language="vbscript" Runat="Server">
'--------------------------------------
'版权说明:
'Author:西楼冷月
'Date:2005-8-21
'HomeSite: |-西楼社区-| //
'E-Mail:ch_56163.com
'QQ:39949376 CMS讨论群:11497202
'version:西楼模板引擎V1.0精简版 XILOU-EasyTemplate v1.0
'--------------------------------------
'函数说明:
'Set_Block(BlkName)
'Set_Bvar(TagName,TagValue)
'Prase_Block(BlkName,BlkList)
'Replace_Block(BlkNameList)
'Set_Var(TagName,TagValue)
'Prase_Temp()
'OutPut()
'--------------------------------------
'变量说明:
'Key(),Value(),Num'//--------单标签保存数组
'BKey(),BValue(),BNum'//--------区块内单标签保存数组
'Temp'//----------模板全局变量
'BeginBlockStart,BeginBlockEnd,EndBlockStart,EndBlockEnd'//----------区块标签设置
'VarBegin,VarEnd'//------------单标签设置
'Re,Match,Mathes
'--------------------------------------
'集合说明:
'Block '//保存替换后的区块内容
'Block_List '//取出区块,保存区块内内容
'BlockNC '//取出区块,保存包括区块标签的整个区块内容
'---------------------------------------
On Error Resume Next
Class EasyTemplate
Private Key(),Value(),Num
Private BKey(),BValue(),BNum
public Block,Block_List,BlockNC
Public Temp
Private BeginBlockStart,BeginBlockEnd,EndBlockStart,EndBlockEnd
Private VarBegin,VarEnd
Private Re,Match,Mathes
'--------------------------------------------------------
Private Sub Class_Initialize
'response.write "西楼模板引擎开始了,哈哈!
"
set Block =CreateObject("Scripting.Dictionary") '追加内容,更新内容
set Block_List =CreateObject("Scripting.Dictionary") '储存区块内容
set BlockNC =CreateObject("Scripting.Dictionary")'//储存包括区块标签的区块内容
set re=New RegExp
redim Key(20)
redim Value(20)
Num=0
redim BKey(20)
redim BValue(20)
BNum=0
redim PBblockName(20)
BlockNum=0
'------------默认标签模式------------
BeginBlockStart="<beginblock:"
BeginBlockEnd=">"
EndBlockStart="<endblock:"
EndBlockEnd=">"
VarBegin="{Tag:"
VarEnd="}"
End Sub
Private Sub Class_Terminate
set Block=nothing
set Block_List=nothing
set BlockNC=nothing
set re=nothing
Erase Key
Erase Value
Erase BKey
Erase BValue
'response.write "西楼模板引擎结束了!"
End Sub
'-------------------------------------------------------
Public Property Let SetTag(BS,BE,ES,EE,BG,ED)'//--------重新设置标签模式
BeginBlockStart= BS
BeginBlockEnd=BE
EndBlockStart=ES
EndBlockEnd=EE
VarBegin=BG
VarEnd=ED
End Property
'-------------------------------------------------------
Public Sub Set_Var(TagName,TagValue) '单标签解析
dim k
if IsArray(TagName) and IsArray(TagValue) then
for k=LBound(TagName) to Ubound(TagName)
Key(Num)=TagName(k)
Value(Num)=TagValue(k)
Num=Num+1
if UBound(Key)=Num then '如果数组太小则动态分配空间
redim preserve Key(Num+20)
redim preserve Value(Num+20)
end if
next
else
Key(Num)=TagName
Value(Num)=TagValue
Num=Num+1
if UBound(Key)=Num then
redim preserve Key(Num+20)
redim preserve Value(Num+20)
end if
end if
End Sub
Public Sub Prase_Temp()'//------------单标签替换
dim i
for i=LBound(Key) to UBound(Value)
Temp=replace(Temp,VarBegin&Key(i)&VarEnd,Value(i))
next
End Sub
'===============区块解析============================
Public Sub Set_BVar(TagName,TagValue) '区块内单标签解析
dim k
if IsArray(TagName) and IsArray(TagValue) then
for k=LBound(TagName) to Ubound(TagName)
BKey(BNum)=TagName(k)
BValue(BNum)=TagValue(k)
BNum=BNum+1
if UBound(BKey)=BNum then '如果数组太小则动态分配空间
redim preserve BKey(BNum+20)
redim preserve BValue(BNum+20)
end if
next
else
BKey(BNum)=TagName
BValue(BNum)=TagValue
BNum=BNum+1
if UBound(BKey)=BNum then
redim preserve BKey(BNum+20)
redim preserve BValue(BNum+20)
end if
end if
End Sub
Public Sub Set_Block(blkname) '设置区块,取出区块内容
re.Global=true
re.IgnoreCase=true
re.Pattern=BeginBlockStart&blkname&BeginBlockEnd&"([\s\S.]*?)"&EndBlockStart&blkname&EndBlockEnd
set matches=re.Execute(Temp)
BlockNC.Add blkname,matches(0)'//建立整个区块名称和对应的键值
match=matches(0).SubMatches(0)
Block_List.Add blkname,match '//建立区块名称和对应的键值
set matches=nothing
End Sub
Public Sub Prase_Block(BlkName)'//---解析区块
dim Blk_Content,tag,k
Blk_Content=Block_List.Item(BlkName)
k=LBound(BKey)
for each tag in BKey
Blk_Content=replace(Blk_Content,VarBegin&tag&VarEnd,BValue(k))
k=k+1
next
redim BKey(20) '重新声明数组
redim BValue(20)
BNum=0 '初始数变为0
if Block.Exists(BlkName) then '//不重复记录内容
dim m:m=Block.Item(BlkName)
Block.Remove (BlkName)
Block.Add BlkName,Blk_Content
else
Block.Add BlkName,Blk_Content
end if
End Sub
'==============================================
Public Sub ClearTag()'//----------清除多余标签
re.Global=true
re.IgnoreCase=true
re.Pattern=BeginBlockStart&"([\s\S.]*?)"&BeginBlockEnd&"([\s\S.]*?)"&EndBlockStart&"([\s\S.]*?)"&EndBlockEnd
set Matches=re.Execute(Temp)
For Each Match In Matches
Temp=Replace(Temp,Match,"")
Next
re.Pattern=VarBegin&"([\s\S.]*?)"&VarEnd
Set Matches=re.Execute(Temp)
For Each Match In Matches
Temp=Replace(Temp,Match,"")
Next
End Sub
'=============================================
Public Sub OutPut()'//----------输出解析后的模板,方法
response.write Temp
End Sub
End Class
</Script>
发布一个简单点的ASP摸板类
80酷酷网 80kuku.com