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





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





but 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 &lt;!--#include file= "filename.asp"--&gt; also works.





Function Includeconvert (oregexp, strFileName, Strblock)


Dim Incstart, Incend, match, omatches, str, code


' Extracts 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, "&lt;!--#include") + 13 ' to find a target string &lt;!--#include exactly 13 characters, so +13


do While incstart&gt;incend+12 ' two reference spacing is a continuous--&gt;&lt;--#,incstart is #include起数13个字符 from the &lt;!--, so it takes at least more than the previous Incend 13-1 conditions of the obtained &gt;incend+12


str = Mid (strblock,incend,incstart-incend-13)


str = replace (str, "" "" "" "" "") ' replaces single double quotes with two double quotes


str = Replace (str, vbcr, "")


str = Replace (str, VBLF, "")


str = Replace (str, VbCrLf, "")


Code = code &amp; VbCrLf &amp; "Response.Write" "" &amp; Str &amp; "" "


incend=instr (Incstart,strblock, "--&gt;") +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 &amp; Include (Mid strFileName, 1, InStrRev (strFileName, "/") &amp; 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, "&lt;!--#include") +13


Loop


str = Mid (strblock,incend)


str = replace (str, "" "" "" "" "") ' replaces single double quotes with two double quotes


str = Replace (str, vbcr, "")


str = Replace (str, VBLF, "")


str = Replace (str, VbCrLf, "")


Code = code &amp; VbCrLf &amp; "Response.Write" "" &amp; Str &amp; "" "


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, "&lt;%") +2


Set re=new RegExp


Do While aspstart&gt;aspend+1


' Traditional Quotes &lt;!--#inclde must be outside the ASP code snippet, so go ahead.


Code = code &amp; Includeconvert (Re, filename, Mid (content,aspend,aspstart-aspend-2))


aspend=instr (aspstart,content, "%&gt;") +2


re.pattern= "^s*=" is replaced with &lt;% = str% &gt; to the standard &lt;%response.write str% &gt;


Code = code &amp; VbCrLf &amp; Re.replace (Mid (content,aspstart,aspend-aspstart-2), "Response.Write") ' ASP block before adding carriage return line To avoid multiple Response.Write errors in the same row between the connecting blocks


aspstart=instr (aspend,content, "&lt;%") +2


Loop


Code = code &amp; 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 for the inclusion of the file with the same path to the main file test pass, not to be included in the file with the main file path of the case for further fault tolerance, limited time, welcome to interested friends 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.