电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> PHP
一个仿phplib的模板类
作者:网友供稿 点击:9
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
用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


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·Windows下的PHP5.0安装配制详解-PHP教程,PHP安装
·PHP在XP下IIS和Apache2服务器上的安装-PHP教程,PHP应用
·最近忙于FTP,好站多多!有好多好东东哦!不敢独享!-PHP教程,PHP基础
·PHP 5.0 Pear安装方法-PHP教程,PHP安装
·PHP开发利器-PRADO 1.6(4)-PHP教程,PHP应用
·Sun Sparc Solaris 2.6 Apache-1.3.12+MySQL-3.23.5+PHP-3.0.15 安装记-PHP教程,PHP应用
·php5学习笔记(转)-PHP教程,PHP应用
·APACHE安装笔记-PHP教程,PHP安装
·PHP.MVC的模板标签系统(四)-PHP教程,PHP应用
·PHP.MVC的模板标签系统(二)-PHP教程,PHP应用

最新文章
·PHP源码-利用 QQWry.Dat 实现 IP 地址高效检索
·Php高手带路--问题汇总解答[2]
·PHPQQ编程(2):取QQ在线状态
·php5手动最简安装方法
·福利彩票幸运号码自动生成器
·PHP开发利器-PRADO 1.6
·在Apache 服务器上启用PHP支持
·Windows2000_pro下安装Apache+PHP4+My
·php文件上传的实现
·PHP开发框架的现状和展望




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号