用php用惯了,用phplib用惯了,没有他觉得很不舒服
上网查找没有人写,自己写了一个,asp水平不高,希望
能有高手指点修改。
<%
====================================
name: template class
purpose: parse and output html page
date: 10.2002
author: pig
email: pigzjq@sina.com
phone: 13910320759
====================================
class template
dim gs_root
dim gs_handle()
dim gs_file()
dim gs_keys()
dim gs_keyvars()
dim gs_vars()
构造函数
private sub template_initialize()
call of_setroot(".")
call of_redimvar()
end sub
function of_redimvar()
redim gs_handle(0)
redim gs_file(0)
redim gs_keys(0)
redim gs_keyvars(0)
redim gs_vars(0)
end function
设置模板存放路径
function of_setroot(byval ps_root)
if ps_root <> "" then
gs_root = ps_root
end if
end function
设置文件名称函数
function of_setfile(byval ps_filehandle,byval ps_filename)
if ps_filename <> "" then
li_maxnum=ubound(gs_handle)
gs_handle(li_maxnum) = ps_filehandle
gs_file(li_maxnum) = gs_root+"/"+ps_filename
li_maxnum=li_maxnum+1
redim preserve gs_handle(li_maxnum)
redim preserve gs_file(li_maxnum)
end if
end function
设置要替换的参数变量
function of_setvar(byval ps_key,byval ps_var)
if ps_key <> "" and ps_var <> "" then
li_keyindex=of_getindex(gs_keys,ps_key)
if li_keyindex="no" then
li_maxnum=ubound(gs_keys)
gs_keys(li_maxnum)=ps_key
gs_keyvars(li_maxnum)="\{" & ps_key & "\}"
gs_vars(li_maxnum)=ps_var
li_maxnum=li_maxnum+1
redim preserve gs_keys(li_maxnum)
redim preserve gs_keyvars(li_maxnum)
redim preserve gs_vars(li_maxnum)
else
gs_keys(li_keyindex)=ps_key
gs_keyvars(li_keyindex)="\{" & ps_key & "\}"
gs_vars(li_keyindex)=ps_var
end if
end if
end function
定义重载文字块儿
function of_setblock(byval ps_parent,byval ps_handle,byval ps_name)
if (not of_loadfile(ps_parent)) then
ls_error="of_loadfile unable to load "+ps_parent
response.write(ls_error)
of_setblock=false
exit function
end if
if ps_name="" then
ps_name=ps_handle
end if
ls_string=of_getvar(ps_parent)
ls_pattern = "<!–\s*begin "&ps_handle&"\s*–>(.*)<!–\s*end "&ps_handle&"\s*–>"
set regex = new regexp
regex.pattern = "\n"
regex.ignorecase = false
regex.global = true
ls_string = regex.replace(ls_string,"")
regex.pattern = ls_pattern
regex.multiline = true
regex.global = false
set matches = regex.execute(ls_string)
ls_string = regex.replace(ls_string,"{"&ps_name&"}")
for each match in matches
ls_value=match.value
next
regex.pattern = "<!–\s*begin "&ps_handle&"\s*–>"
regex.ignorecase = false
regex.global = true
ls_value = regex.replace(ls_value,"")
regex.pattern = "<!–\s*end "&ps_handle&"\s*–>"
regex.ignorecase = false
regex.global = true
ls_value = regex.replace(ls_value,"")
call of_setvar(ps_handle,ls_value)
call of_setvar(ps_parent,ls_string)
end function
装载变量内容
function of_loadfile(byval ps_handle)
li_keyindex=of_getindex(gs_keys,ps_handle)
if li_keyindex = "no" then
li_fileindex=of_getindex(gs_handle,ps_handle)
if li_fileindex = "no" then
ls_error="loadfile:"+ps_handle+" is not a valid handle."
response.write(ls_error)
of_loadfile=false
exit function
end if
ls_filename=gs_file(li_fileindex)
ls_filename=server.mappath(ls_filename)
set myfileobject=server.createobject("scripting.filesystemobject")
li_exist=myfileobject.fileexists(ls_filename)
if li_exist then
set mytextfile=myfileobject.opentextfile(ls_filename)
ls_file=mytextfile.readall()
mytextfile.close
call of_setvar(ps_handle,ls_file)
else
ls_error="loadfile:"+ls_filename+" is not a valid file or path."
response.write(ls_error)
of_loadfile=false
exit function
end if
end if
of_loadfile=true
end function
变量替换过程
function of_replace(byval ps_handle)
if (not of_loadfile(ps_handle)) then
ls_error="of_replace: unable to load "+ps_handle
response.write(ls_error)
of_replace=false
exit function
end if
ls_str=of_getvar(ps_handle)
li_minindex=lbound(gs_keys)
li_maxindex=ubound(gs_keys)
for i=li_minindex to li_maxindex
set regex = new regexp
regex.pattern = gs_keyvars(i)
regex.ignorecase = true
ls_str = regex.replace(ls_str, gs_vars(i))
next
of_replace=ls_str
end function
获取存储变量
function of_getvar(byval ps_keyname)
li_keyindex=of_getindex(gs_keys,ps_keyname)
if(li_keyindex="no") then
of_getvar=""
else
ls_varname=gs_vars(li_keyindex)
of_getvar=ls_varname
end if
end function
查找数组中相应字符串的索引值
function of_getindex(byval ps_array,byval ps_handle)
li_minindex=lbound(ps_array)
li_maxindex=ubound(ps_array)
li_index="no"
for i=li_minindex to li_maxindex
if (ps_array(i)=ps_handle) then
li_index=i
exit for
end if
next
of_getindex=li_index
end function
分析变量
function of_parse(byval ps_target,byval ps_handle,byval ps_append)
ls_string = of_replace(ps_handle)
if ps_append=true then
ls_org = of_getvar(ps_target) & ls_string
if pi_time=2 then
response.write(ls_string)
exit function
end if
call of_setvar(ps_target,ls_org)
else
call of_setvar(ps_target,ls_string)
end if
end function
function of_print(byval ps_keyname)
li_keyindex=of_getindex(gs_keys,ps_keyname)
ls_varstring=gs_vars(li_keyindex)
set regex = new regexp
regex.pattern = "{[^ \t\r\n}]+}"
regex.ignorecase = true
ls_varstring = regex.replace(ls_varstring, "")
of_print=ls_varstring
end function
end class
%>
调用页面:
<%
dim c_html
set c_html = new template
call c_html.of_redimvar()
call c_html.of_setfile("main","index.htm")
call c_html.of_setblock("main","block_test","mytest")
call c_html.of_setvar("test","testblock1")
call c_html.of_parse("mytest","block_test",true)
call c_html.of_setvar("test","testblock2")
call c_html.of_parse("mytest","block_test",true)
call c_html.of_setvar("test1","success")
call c_html.of_parse("main_output","main",false)
ls_result=c_html.of_print("main_output")
response.write(ls_result)
%>
模板文件:
{test1}
<table border=1>
<!– begin block_test –>
<tr><td width=250 class=h1 height=40 bgcolor=#f0f0f0 align=center>
{test}
</td></tr>
<!– end block_test –>
</table>
希望对用惯了phplib中的强大功能的朋友能有所帮助
我希望交接所有php以及asp的爱好者
我的qq:9457009
