對ASP 動態包含檔案方法的改進

來源:互聯網
上載者:User

 ASP 本身不支援動態包含檔案,現在的動態包含是通過 FSO 把被包含的檔案合并到主檔案裡再運行。以下也有把形如 <!--#include file="filename.asp" --> 的普通包含檔案方式稱作“傳統引用”,用函數實現的動態包含檔案稱作“動態引用”。常見的程式如下:

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")

  以上範例引自 http://www.blueidea.com/tech/program/2003/101.asp

  但這處函數在處理補包含的檔案中還有包含檔案時就不靈了。我在以上函數的基礎上改進出來如下函數,在被包含檔案中還有普通的包含檔案 <!--#include file="filename.asp" --> 也可正常運行。

Function includeconvert(oRegExp, strFilename, strBlock)
Dim incStart, incEnd, match, oMatches, str, code
'用提取ASP代碼的相同方式提取出include 部分的檔案名稱,其餘部分原樣輸出
code = ""
incEnd = 1
incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 '要找個目標字串<!--#include 正好是13個字元,所以要+13
Do While incStart>incEnd+12 '兩個引用間距最小就是連續的--><--#,incStart是從<!--#include起數13個字元,所以要比前一個incEnd要至少多 13-1 得到的>incEnd+12的條件
str = Mid(strBlock,incEnd,incStart-incEnd-13)
str = Replace(str, """", """""") '把單個雙引號換成兩個雙引號
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",捕捉類型及檔案名稱兩個子串
Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3))
Set match = oMatches(0) '確定只有一組捕捉時,要得到這一組匹配的子串,可以這樣做,省去用 For Each match In oMatches …… Next
code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) 'Mid(filename, 1, InStrRev(filename, "/")) 是在被引用的子檔案名稱有路徑時,把路徑提取出來,加在子檔案中傳統引用的檔案名稱前面,以找到正確的開啟檔案路徑,因為動態引用時的檔案路徑是相對主檔案而言的。要第二個匹配子串用SubMatches(1)
incStart = InStr(incEnd,strBlock,"<!--#include ")+13
Loop
str = Mid(strBlock,incEnd)
str = Replace(str, """", """""") '把單個雙引號換成兩個雙引號
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
'傳統引用<!--#inclde 肯定是在ASP程式碼片段以外的,所以先轉。
code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))
aspEnd=InStr(aspStart,content,"%>")+2
re.pattern="^s*=" '這段正則替換原來是把 <% = str % > 換回成標準的 <%Response.Write str % >
code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") 'ASP塊前面再加斷行符號換行,以避免串連塊之間多個 Response.Write在同一行的錯誤
aspStart=InStr(aspEnd,content,"<%")+2
Loop
code = code & includeconvert (re, filename, Mid(content,aspEnd))
Set re=nothing
include = code
End Function

  方便起見,以上函數最終返回的是整合了包含檔案的整個 ASP 代碼,使用時還要再用 Execute 執行之,即使用時需要:Execute(include("file.asp"))。

  以上函數對被包含檔案與主檔案同一路徑時測試通過,未對被包含檔案與主檔案路徑不同的情況做進一步容錯,時間有限,歡迎有興趣的朋友提出意見和改進。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.