Improvement of ASP dynamic inclusion file

Source: Internet
Author: User
Tags contains reference setf
Dynamic

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. There are also the common include files in the form <!--#includefile = "filename.asp"--> called "traditional references", and dynamic inclusion files implemented by functions are called "Dynamic references". The common procedures are as follows:
Functioninclude (filename)
Dimre,content,fso,f,aspstart,aspend

Setfso=createobject ("Scripting.FileSystemObject")
Setf=fso. OpenTextFile (Server.MapPath (filename))
Content=f.readall
F.close
Setf=nothing
Setfso=nothing

Setre=newregexp
Re.pattern= "^s*="
Aspend=1
Aspstart=instr (aspend,content, "<%") +2
Dowhileaspstart>aspend+1
Response.writemid (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.writemid (Content,aspend)
Setre=nothing
Endfunction

Usage examples: include ("youinc.asp")

Examples cited above

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 <!--in the included file #includefile = "filename.asp"--> can also run normally.

Functionincludeconvert (Oregexp,strfilename,strblock)
Dimincstart,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正好是13个字符, so be +13
Dowhileincstart>incend+12 ' Two reference spacing is a continuous--><--#,incstart from the <!--#include起数13个字符, So it takes at least 13-1 more >incend+12 to get the incend than the previous one.
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
Setomatches=oregexp.execute (Mid (strblock,incstart,incend-incstart-3))
Setmatch=omatches (0) ' to determine that only a set of captures, to get this set of matching substrings, you can do this, omit the use of foreachmatchinomatches ... Next
Code=code&include (Mid Strfilename,1,instrrev (strFileName, "/") &match. Submatches (1)) ' Mid (Filename,1,instrrev (filename, "/") is to extract the path when the referenced child file has a path, and add 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
Endfunction
Functioninclude (filename)
Dimre,content,fso,f,aspstart,aspend,code
Setfso=createobject ("Scripting.") FileSystemObject ")
Setf=fso. OpenTextFile (Server.MapPath (filename))
Content=f.readall
F.close
Setf=nothing
Setfso=nothing

Code= ""
Aspend=1
Aspstart=instr (aspend,content, "<%") +2
Setre=newregexp
Dowhileaspstart>aspend+1
' Traditional quotes <!--#inclde肯定是在ASP代码段以外的, so turn first.
Code=code&includeconvert (Re,filename,mid (content,aspend,aspstart-aspend-2))
Aspend=instr (aspstart,content, "%>") +2
The re.pattern= "^s*=" is replaced by the original <%=str%> change back into the standard <%Response.Writestr%>
Code=code&vbcrlf&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 connection blocks
Aspstart=instr (aspend,content, "<%") +2
Loop
Code=code&includeconvert (Re,filename,mid (content,aspend))
Setre=nothing
Include=code
Endfunction

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

Related Article

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.