Dynamic one, dynamic include files
This article assumes that the reader is already familiar with the use of HTML, ASP, and ActiveX objects. Here we will implement the "Dynamic inclusion" feature using the FileSystemObject and TextStream objects, that is, to implement dynamic include directives.
Usually the dynamic inclusion we envision is to save the name of the containing file with a variable, and then pass the variable to the include directive. It is believed that many of us have tried to use dynamically contained files in our ASP applications, but found that the server does not directly support dynamic inclusion.
The first thing to understand is that the following code is not dynamically contained:
<%
' Declares the variable used to save the file name
Dim MyFile
' Save the name of the containing file in the variable
MyFile = Request ("Somefilename")
' Pass the variable that holds the file name to the include directive
%>
<!--#include file=<%=myfile%>-->
The above code does not implement dynamic inclusion because the ASP's handling of the include directive is earlier than the scripting code. If we execute the above code, we will be prompted incorrectly that the containing file cannot be found.
One of the motivations for using the include file is to make it a container for static HTML content, and then refer to the file with the include directive. Static HTML in the include file is usually not altered, such as standard headers and footers. But there are times when we need to provide different content based on user input or other conditions. In order not to create a complete page for each possible scenario, the concept of a "dynamic include" file appears. However, as explained above, the seemingly most reasonable approach to implementation is actually useless.
To solve this problem, we can use the FileSystemObject object to read the target inclusion file and save it to a string variable, and then insert the string variable into the page that is sent to the client's browser. The following getfilecontents function helps implement this process by reading the file specified in the parameter and then returning its contents as a string.
<%
' Pass the file name to the function
Function getfilecontents (Strincludefile)
Dim objFSO
Dim Objtext
Dim strpage
' Initialize the FileSystemObject object
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
' Open the file and pass it to the TextStream object (Objtext). of the server object
' MapPath function to get the physical path of a file
Set Objtext = objFSO.OpenTextFile (Server.MapPath (strincludefile))
' reads and returns the contents of the file as a string
Getfilecontents = Objtext.readall
Objtext.close
Set Objtext = Nothing
Set objFSO = Nothing
End Function
%>
You can use this function to dynamically include files. First, we read the master page (the template file that contains the page layout and all the static content) and save it to a string variable, then read the containing file and save it as a string variable, and then insert the variable containing the contents of the file into the contents of the main page.
Second, application examples
Let's take a look at the template file first. The following code contains an HTML comment "<!--include file here-->", and we will replace the HTML annotation with the contents of the containing file.
<body>
<table width= "border=" 1 ">
<tr>
<td>
<!--INCLUDE FILE here-->
</td>
</tr>
</table>
</body>
Let's take a look at some of the included files used in this example. The first include file is the default include file. The default include file is a form that allows the user to choose to open one of the other three included files. Note that this form does not specify an "action" attribute, which means that the form will be submitted to itself (that is, reopen dynamicin3.asp).
<!--BEGIN DEFAULT INCLUDE-->
<form method= "POST" >
<p>
<select Id=cbofile name=cbofile>
<option value= "Includefile1.inc" > File #1 </option>
<option value= "Includefile2.inc" > File #2 </option>
<option value= "Includefile3.inc" > File #3 </option>
</select>
<input type= "Submit" value= "Submit" >
</p>
</form>
<!--end DEFAULT INCLUDE-->
As a simple gauge, the contents of the other three included files are simple:
<!--BEGIN INCLUDE FILE #1-->
<H2 style= "color:red" > File #1 <br>
<a href= "dynamicinc3.asp" > Return to Default page </a>
<!--end INCLUDE FILE #1-->
<!--BEGIN INCLUDE FILE #2-->
<H2 style= "Color:green" > File #2 <br>
<a href= "dynamicinc3.asp" > Return to Default page </a>
<!--end INCLUDE FILE #2-->
<!--BEGIN INCLUDE FILE #3-->
<H2 style= "Color:blue" > File #3 <br>
<a href= "dynamicinc3.asp" > Return to Default page </a>
<!--end INCLUDE FILE #3-->
The following is the code for the Dynamicinc3.asp page and its description.
<%
'-------------------------------------------------------------
' Getfilecontents function in front of ASP file
'-------------------------------------------------------------
' Declares a variable that holds the main page and contains the contents of the file
Dim Strmain, Strinclude
' Read the contents of the main page and save it to the Strmain variable
Strmain = getfilecontents ("Maintemplate.inc")
' Check to see if the Cbofile selection box has been selected. If so, read the requested file
' Otherwise, read the default include file
If Request.Form ("cbofile") = "" Then
Strinclude = getfilecontents ("Includedefault.inc")
Else
Strinclude = getfilecontents (Request.Form ("Cbofile"))
End If
' Read the appropriate inclusion file into the variable strinclude
' Insert it into the Strmain file using the Replace function
Strmain = replace (Strmain, "<!--INCLUDE FILE here-->", Strinclude)
' Send the results to the client
Response.Write Strmain
%>
This example works well and achieves the goal of dynamically containing files from an effect. But instead of using the include directive, it uses a FileSystemObject to replace it.
To run this example, right-click the page and view the source file, and you can see that the default include file is inserted in the page. Then, select a include file from the select list. After submitting the form to reload the page, and then looking at the page's source file, you can see that the included file you just selected is inserted at this time.
This technique is useful when we want to detach the layout of the page and its contents. We can create a template describing the layout of the site, and then make the content of the site in the form of a file, finally using ASP can easily combine the two!