下面这段代码谁能详细讲解一下(动态包含不同的文件)

theoffspring 2004-02-07 08:28:00
根据需要动态include不同的文件 qiushuiwuhen(原作)

关键字 秋水无恨 动态 include 文件



受<! #include file="filename.asp" --> 宏限制
必须存在该文件并且会预先编译(不管前面是否加以条件)

经常有这样的要求,根据不同的需求要求include不同的文件
如各个人的不同设置,所以要求能动态include文件。

代码如下:

Function include(filename)
Dim re,content,fso,f,aspStart,aspEnd

set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
set f=nothing
set fso=nothing

set re=new RegExp
re.pattern="^\s*="
aspEnd=1
aspStart=inStr(aspEnd,content,"<%")+2
do while aspStart>aspEnd+1
Response.write Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content,"%\>")+2
Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))
aspStart=inStr(aspEnd,content,"<%")+2
loop
Response.write Mid(content,aspEnd)
set re=nothing
End Function

使用范例:

include("youinc.asp")

有个问题:我调用此函数的时候,包含文件里的一般语句可以执行,但过程却不好用,为什么?
包含文件如下:
<!--conn.asp-->
if application("cn_db")="" then
set connstr=Server.CreateObject("connstr.cls")
application("cn_db")=connstr.outpath("cn")
set connstr=nothing
end if
conn_sql=application("cn_db")
if not isobject(cnn) then
set cnn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
cnn.open conn_sql
end if
sub rsclose(byref objRs)
if isobject(objRs) then
if objRs.state=0 then
set objRs=nothing
elseif objRs.state=1 then
objRs.close
set objRs=nothing
end if
objRs=null
end if
end sub
sub cnnclose(byref objCnn)
if isobject(objCnn) then
if objCnn.state=0 then
set objCnn=nothing
elseif objCnn.state=1 then
objCnn.close
set objCnn=nothing
end if
objCnn=null
end if
end sub

...全文
43 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复

28,407

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧