分享一个好东东,动态Include文件 (Dynamic File Includes)

2018-09-06 12:47

阅读:421

  早在03年就在蓝色理想上看到过动态Include的文章,当时已经觉得很厉害,但实际应用了一下,不方便而且Include的效果不好.

后来又在一网站上看到了改进版的,但是也不太好用~~~

哎,当时我真是觉得有点想放弃ASP了,但是由于公司还是用ASP来开发,我也是没有办法...

今天,我一定要记住今天~~~在国外的一个网站上我竟然发现了这样一个好东东,太棒了~~~Greatworks!!!

以前试的一些动态Include代码,都无法Include一个类,甚至函数~~~又或者Include文件中的Include无法被包含...

现在这个鬼佬(dselkirk)写的类可以为我们做到这些了~~~
复制代码 代码如下:
<%
publicinclude,include_vars
setinclude=newcls_include

classcls_include

privatesubclass_initialize()
setinclude_vars=server.createobject(scripting.dictionary)
endsub
privatesubclass_deactivate()
arr_variables.removeall
setinclude_vars=nothing
setinclude=nothing
endsub

publicdefaultfunctioninclude(byvalstr_path)
dimstr_source
ifstr_path<>then
str_source=readfile(str_path)
ifstr_source<>then
processincludesstr_source
convert2codestr_source
formatcodestr_source
ifstr_source<>then
ifrequest.querystring(debug)=1then
response.writestr_source
response.end
else
executeglobalstr_source
include_vars.removeall
endif
endif
endif
endif
endfunction

privatesubconvert2code(str_source)
dimi,str_temp,arr_temp,int_len
ifstr_source<>then
ifinstr(str_source,%&>)>instr(str_source,<&%)then
str_temp=replace(str_source,<&%,%)
str_temp=replace(str_temp,%&>,)
ifleft(str_temp,1)=thenstr_temp=right(str_temp,len(str_temp)-1)
ifright(str_temp,1)=thenstr_temp=left(str_temp,len(str_temp)-1)
arr_temp=split(str_temp,)
int_len=ubound(arr_temp)
if(int_len+1)>0then
fori=0toint_len
str_temp=trim(arr_temp(i))
str_temp=replace(str_temp,vbcrlf&vbcrlf,vbcrlf)
ifleft(str_temp,2)=vbcrlfthenstr_temp=right(str_temp,len(str_temp)-2)
ifright(str_temp,2)=vbcrlfthenstr_temp=left(str_temp,len(str_temp)-2)
ifleft(str_temp,1)=%then
str_temp=right(str_temp,len(str_temp)-1)
ifleft(str_temp,1)==then
str_temp=right(str_temp,len(str_temp)-1)
str_temp=response.write&str_temp
endif
else
ifstr_temp<>then
include_vars.addi,str_temp
str_temp=response.writeinclude_vars.item(&i&)
endif
endif
str_temp=replace(str_temp,chr(34)&chr(34)&&,)
str_temp=replace(str_temp,&&chr(34)&chr(34),)
ifright(str_temp,2)<>vbcrlfthenstr_temp=str_temp
arr_temp(i)=str_temp
next
str_source=join(arr_temp,vbcrlf)
endif
else
ifstr_source<>then
include_vars.addvar,str_source
str_source=response.writeinclude_vars.item(var)
endif
endif
endif
endsub

privatesubprocessincludes(str_source)
dimint_start,str_path,str_mid,str_temp
str_source=replace(str_source,<!--#,<!--#)
int_start=instr(str_source,<!--#include)
str_mid=lcase(getbetween(str_source,<!--#include,-->))
dountilint_start=0
str_mid=lcase(getbetween(str_source,<!--,-->))
int_start=instr(str_mid,#include)
ifint_start>0then
str_temp=lcase(getbetween(str_mid,chr(34),chr(34)))
str_temp=trim(str_temp)
str_path=readfile(str_temp)
str_source=replace(str_source,<!--&str_mid&-->,str_path&vbcrlf)
endif
int_start=instr(str_source,#include)
loop
endsub

privatesubformatcode(str_code)
dimi,arr_temp,int_len
str_code=replace(str_code,vbcrlf&vbcrlf,vbcrlf)
ifleft(str_code,2)=vbcrlfthenstr_code=right(str_code,len(str_code)-2)
str_code=trim(str_code)
ifinstr(str_code,vbcrlf)>0then
arr_temp=split(str_code,vbcrlf)
fori=0toubound(arr_temp)
arr_temp(i)=ltrim(arr_temp(i))
ifarr_temp(i)<>thenarr_temp(i)=arr_temp(i)&vbcrlf
next
str_code=join(arr_temp,)
arr_temp=vbnull
endif
endsub

privatefunctionreadfile(str_path)
dimobjfso,objfile
ifstr_path<>then
ifinstr(str_path,:)=0thenstr_path=server.mappath(str_path)
setobjfso=server.createobject(scripting.filesystemobject)
ifobjfso.fileexists(str_path)then
setobjfile=objfso.opentextfile(str_path,1,false)
iferr.number=0then
readfile=objfile.readall
objfile.close
endif
setobjfile=nothing
endif
setobjfso=nothing
endif
endfunction

privatefunctiongetbetween(strdata,strstart,strend)
dimlngstart,lngend
lngstart=instr(strdata,strstart)+len(strstart)
if(lngstart<>0)then
lngend=instr(lngstart,strdata,strend)
if(lngend<>0)then
getbetween=mid(strdata,lngstart,lngend-lngstart)
endif
endif
endfunction

endclass
%>


评论


亲,登录后才可以留言!