关于通过vbs类处理模板实现代码与界面分离的程序,建议入精华备查。有演示
2024-07-21 02:15:35
供稿:网友
文章标题:vbs类处理模板实现代码与界面分离
'作者:yanek
'email:[email protected]
本程序通过vbs类处理模板实现代码与界面分离的程序,主要有下面文件组成
index.asp,parse_cls.asp,template.html
下面是代码
1。index.asp 调用vbs类处理模板
<%
'作者:yanek
'email:[email protected]
'--- ---
' index.asp
'------ ----
' (c)2000 james q. stansfield ([email protected])
' 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 i've 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:[email protected]
'--- ---
' 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 we've 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 it's 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("scriptin