The improvement of ASP dynamic inclusion file method

Source: Internet
Author: User

The ASP itself does not support dynamic inclusion files, and the current dynamic inclusion is through the FSO to merge the included files into the main file and then run. The following are also the common inclusion files in the form <!--#include file= "filename.asp"-->, called "Traditional references", and dynamic inclusion files implemented by functions are called "Dynamic references". The common procedures are as follows:

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

Usage examples: include ("youinc.asp")

The above examples are quoted from http://www.blueidea.com/tech/program/2003/101.asp

However, this function does not work when the file containing the complement contains files. On the basis of the above functions, I have improved the following functions, including the normal inclusion file <!--#include file= "filename.asp"--> also works.

Function Includeconvert (Oregexp, strFileName, Strblock)
Dim Incstart, Incend, match, omatches, str, code
' Extract the file name of the include part in the same way as the ASP code, and the remainder is output
Code = ""
Incend = 1
Incstart = InStr (Incend,strblock, "<!--#include") + 13 ' to find a target string <!--#include exactly 13 characters, so +13
The minimum incstart>incend+12 ' Two reference spacing is the continuous--><--#,incstart from the <!--#include起数13个字符, so at least 13-1 more than the previous one incend The condition of getting the >incend+12
str = Mid (STRBLOCK,INCEND,INCSTART-INCEND-13)
str = replace (str, "" "" "" "" "" ")" replace single double quotes with two double quotes
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+) =" "([^"]+) "" "" Match file= "filename.ext" or virtual= "Virtualname.ext", capture type and filename two substrings
Set omatches = Oregexp.execute (Mid (strblock,incstart,incend-incstart-3))
Set match = omatches (0) ' to determine that only a set of captures, to get this set of matching substrings, you can do this, omit the for every match in omatches ... Next
Code = code & Include (Mid strFileName, 1, InStrRev (strFileName, "/") & match. Submatches (1)) ' Mid (filename, 1, InStrRev (filename, "/") extracts the path when the referenced child file name has a path, and adds it to the file name of the traditional reference in the child file to find the correct open file path. Because the file path for a dynamic reference is relative to the primary file. To a second matching substring with submatches (1)
Incstart = InStr (Incend,strblock, "<!--#include") +13
Loop
str = Mid (strblock,incend)
str = replace (str, "" "" "" "" "" ")" replace single double quotes with two double quotes
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
' Traditional references <!--#inclde must be outside the ASP code snippet, so go ahead.
Code = code & Includeconvert (Re, filename, Mid (content,aspend,aspstart-aspend-2))
Aspend=instr (aspstart,content, "%\>") +2
Re.pattern= "^\s*=" is replaced by the original <% = str% > to return to the standard <%response.write str% >
Code = code & VbCrLf & Re.replace (Mid (content,aspstart,aspend-aspstart-2), "Response.Write") ' ASP block before adding a carriage return line, To avoid multiple Response.Write errors in the same row between the connection blocks
Aspstart=instr (aspend,content, "<%") +2
Loop
Code = code & Includeconvert (Re, filename, Mid (content,aspend))
Set re=nothing
Include = code
End Function

For convenience, the above function ultimately returns the entire ASP code that contains the file, and is executed with execute even when it is needed: Execute (Include ("file.asp")).

The above function tests the included file with the same path as the primary file, does not make further fault tolerance for the contained file and the main file path, and the time is limited, and welcome friends who are interested to make comments and improvements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.