asp動(dòng)態(tài)include文件
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
ASP 本身不支持動(dòng)態(tài)包含文件,現(xiàn)在的動(dòng)態(tài)包含是通過(guò) FSO 把被包含的文件合并到主文件里再運(yùn)行。以下也有把形如 <!--#include file="filename.asp" --> 的普通包含文件方式稱作“傳統(tǒng)引用”,用函數(shù)實(shí)現(xiàn)的動(dòng)態(tài)包含文件稱作“動(dòng)態(tài)引用”。常見(jiàn)的程序如下:
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") 但這處函數(shù)在處理補(bǔ)包含的文件中還有包含文件時(shí)就不靈了。我在以上函數(shù)的基礎(chǔ)上改進(jìn)出來(lái)如下函數(shù),在被包含文件中還有普通的包含文件 <!--#include file="filename.asp" --> 也可正常運(yùn)行。 Function includeconvert(oRegExp, strFilename, strBlock) Dim incStart, incEnd, match, oMatches, str, code '用提取ASP代碼的相同方式提取出include 部分的文件名,其余部分原樣輸出 code = "" incEnd = 1 incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 '要找個(gè)目標(biāo)字符串<!--#include 正好是13個(gè)字符,所以要+13 Do While incStart>incEnd+12 '兩個(gè)引用間距最小就是連續(xù)的--><--#,incStart是從<!--#include起數(shù)13個(gè)字符,所以要比前一個(gè)incEnd要至少多 13-1 得到的>incEnd+12的條件 str = Mid(strBlock,incEnd,incStart-incEnd-13) str = Replace(str, """", """""") '把單個(gè)雙引號(hào)換成兩個(gè)雙引號(hào) str = Replace(str, VbCr, "") str = Replace(str, VbLf, "") str = Replace(str, VbCrLf, "") code = code & VbCrLf & "Response.Write """ & str & """" incEnd=InStr(incStart,strBlock,"-->")+3 oRegExp.pattern="(\w+)=""([^""]+)""" '匹配 file="filename.ext" 或 virtual="virtualname.ext",捕捉類型及文件名兩個(gè)子串 Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3)) Set match = oMatches(0) '確定只有一組捕捉時(shí),要得到這一組匹配的子串,可以這樣做,省去用 For Each match In oMatches …… Next code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) 'Mid(filename, 1, InStrRev(filename, "/")) 是在被引用的子文件名有路徑時(shí),把路徑提取出來(lái),加在子文件中傳統(tǒng)引用的文件名前面,以找到正確的打開(kāi)文件路徑,因?yàn)閯?dòng)態(tài)引用時(shí)的文件路徑是相對(duì)主文件而言的。要第二個(gè)匹配子串用SubMatches(1) incStart = InStr(incEnd,strBlock,"<!--#include ")+13 Loop str = Mid(strBlock,incEnd) str = Replace(str, """", """""") '把單個(gè)雙引號(hào)換成兩個(gè)雙引號(hào) str = Replace(str, VbCr, "") str = Replace(str, VbLf, "") str = Replace(str, VbCrLf, "") code = code & VbCrLf & "Response.Write """ & str & """" includeconvert = code End Function Function include(filename) Dim re, content, fso, f, aspStart, aspEnd, code Set fso=CreateObject("scripting.FileSystemObject") Set f=fso.OpenTextFile(Server.MapPath(filename)) content=f.ReadAll f.close Set f=nothing Set fso=nothing code = "" aspEnd=1 aspStart=InStr(aspEnd,content,"<%")+2 Set re=new RegExp Do While aspStart>aspEnd+1 '傳統(tǒng)引用<!--#inclde 肯定是在ASP代碼段以外的,所以先轉(zhuǎn)。 code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2)) aspEnd=InStr(aspStart,content,"%\>")+2 re.pattern="^\s*=" '這段正則替換原來(lái)是把 <% = str % > 換回成標(biāo)準(zhǔn)的 <%Response.Write str % > code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") 'ASP塊前面再加回車換行,以避免連接塊之間多個(gè) Response.Write在同一行的錯(cuò)誤 aspStart=InStr(aspEnd,content,"<%")+2 Loop code = code & includeconvert (re, filename, Mid(content,aspEnd)) Set re=nothing include = code End Function 方便起見(jiàn),以上函數(shù)最終返回的是整合了包含文件的整個(gè) ASP 代碼,使用時(shí)還要再用 Execute 執(zhí)行之,即使用時(shí)需要:Execute(include("file.asp"))。 以上函數(shù)對(duì)被包含文件與主文件同一路徑時(shí)測(cè)試通過(guò),未對(duì)被包含文件與主文件路徑不同的情況做進(jìn)一步容錯(cuò),時(shí)間有限,歡迎有興趣的朋友提出意見(jiàn)和改進(jìn)。 該文章在 2010/9/2 15:01:28 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |