文章标题:vbs类处理模板实现代码与界面分离
作者:yanek
email:aspboy@263.net
本程序通过vbs类处理模板实现代码与界面分离的程序,主要有下面文件组成
index.asp,parse_cls.asp,template.html
下面是代码
1。index.asp 调用vbs类处理模板
<%
作者:yanek
email:aspboy@263.net
— —
index.asp
—— —-
(c)2000 james q. stansfield (james.stansfield@iridani.com)
this code is free for use by anyone. it is meant as a learning tool and can be passed along in any format.
option explicit
%>
<!–#include file="parse_cls.asp"–>
<%
declare our variables
dim g_opagegen
cerate the class object
set g_opagegen = new parsetmpl
set the template file
g_opagegen.templatefile = "template.html"
add some custom tags to the dictionary
g_opagegen.addtoken "title", "template example"
g_opagegen.addtoken "copyright", "this is mine! all mine!"
g_opagegen.addtoken "quote", """tell jabba ive got his money!""<br>–han solo, star wars 1977"
g_opagegen.addtoken "menu", "home page<br>news page<br>link page"
g_opagegen.addtoken "content", "welcome to my home page… yadda yadda yadda!"
generate the page
g_opagegen.generatehtml
destroy our objects
set g_opagegen = nothing
%>
2。parse_cls.asp 处理模板类文件
<%
作者:yanek
email:aspboy@263.net
— —
parse_cls.asp
this code is free for use by anyone. it is meant as a learning tool and can be passed along in any format.
class parsetmpl
dimension variables
private g_stmplfile
private g_odict
private g_bfile
private sub class_initialize
create the scripting.dictionary object,
set the compare mode to 1 so that it is case insensitive.
also flag a boolean file so we know whether our file is there or not.
set g_odict = createobject("scripting.dictionary")
g_odict.comparemode = 1
g_bfile = false
end sub
private sub class_terminate
destroy our object.
set g_odict = nothing
end sub
public property let templatefile(infile)
a file & path must be specified for the routine to work.
g_stmplfile = server.mappath(infile)
end property
private property get templatefile()
templatefile = g_stmplfile
end property
public sub addtoken(intoken, invalue)
this property allows us to set our tokens.
g_odict.add intoken, invalue
end sub
public sub generatehtml
this is the main, and only public method of the class.
this method will check whether weve specified a file or not.
check for the files existance if we have specified it.
if the file exists we will open it and dumps its contents into an array.
the array is split on vbcrlf to make it more manageable.
if len(g_stmplfile) > 0 then
dim l_ofso, l_ofile, l_arrfile
set l_ofso = createobject("scripting.filesystemobject")
if l_ofso.fileexists(templatefile) then
g_bfile = true
set l_ofile = l_ofso.opentextfile(g_stmplfile)
l_arrfile = split(l_ofile.readall, vbcrlf)
l_ofile.close
set l_ofile = nothing
end if
set l_ofso = nothing
else
filename was never passed!
end if
if we have a file, we will prase it line by line
by sending each line to parsetmpl()
if g_bfile then
dim l_sline
for each l_sline in l_arrfile
parsetmpl(l_sline)
next
else
error file not found!
end if
end sub
private sub parsetmpl(inline)
this is a reasonably complex piece of code that looks for any text that is bounded by [% and %]…
if it finds one it takes the first part of the line up to the [% and displays it to the user.
it then passes the bounded text to gettoken.
the it send the text that resides past the %] to itself(parsetmpl), that way it can check for another
token on the same line.
if the routine doesnt find a [% in the line it simply displays it for the user.
dim l_ia, l_ib, l_sline, l_sline2, l_stoken
l_ia = instr(inline, "][%")
if l_ia > 0 then
l_sline = left(inline, l_ia – 1)
l_ib = instr(inline, "%]")
l_sline2 = mid(inline, l_ib + 2)
l_stoken = trim(mid(inline, l_ia + 2, (l_ib – l_ia – 2)))
response.write(l_sline & vbcrlf)
gettoken(l_stoken)
parsetmpl(l_sline2)
else
response.write(inline & vbcrlf)
end if
end sub
private sub gettoken(intoken)
this routine checks the dictionary for the text passed to it.
if it finds a key in the dictionary it display the value to the user.
if not, by default it will display the full token in the html source so that you can debug your templates.
if g_odict.exists(intoken) then
response.write(g_odict.item(intoken) & vbcrlf)
else
response.write("<!–[%" & intoken & "%]–>" & vbcrlf)
end if
end sub
end class
%>
3.template.html 模板文件
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>[%title%]</title>
</head>
<body>
<table border="0" width="785" align="center" cellspacing="0" cellpadding="0" class="largetable">
<tr>
<td rowspan="2" height="100%" width="150" valign="top" align="right">[%menu%]</td>
<td valign="top" width="635" class="caption" align="center">[%quote%]</td>
</tr>
<tr>
<td valign="top" bgcolor="#b0c4de" width="635" class="layout">[%content%]</td>
</tr>
<tr>
<td colspan="2" width="785" align="center" class="caption">[%copyright%]</td>
</tr>
</table>
</body>
</html>
演示地址:http://www.cnaspol.com/tmpcode/index.asp
