1. dynamically include files
This document assumes that you are familiar with the use of HTML, ASP, and ActiveX objects. Here, we will use FileSystemObject and textstream objects to implement the "dynamic inclusion" function, that is, to implement dynamic include commands.
Generally, the concept of dynamic inclusion is to use a variable to save the name of the file to be included, and then pass the variable into the include command. I believe that many of us once wanted to use dynamic inclusion files in their ASP applications, but eventually found that the server does not directly support dynamic inclusion.
The following code cannot be dynamically included:
<%
'Declare the variable used to save the file name
Dim myfile
'Save the name of the file in the variable.
Myfile = request ("somefilename ")
'Pass variables with file names to the include command
%>
<! -- # Include file = <% = myfile %> -->
The above Code cannot be dynamically included because ASP processes the include instruction earlier than the script code. If we execute the above Code, the system will prompt an error saying that the contained file cannot be found.
One of the motivations for using include files is to use it as a container for Static HTML content, and then reference this file using the include command. Static HTML in a file is usually not modified, such as the standard page header and footer. However, sometimes 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 "dynamic inclusion" file emerged. However, as described above, it seems that the most reasonable implementation method is actually useless.
To solve this problem, we can use the FileSystemObject object to read the target contained file and save it to the string variable, and then insert the string variable into the page sent to the client browser. The following getfilecontents function helps implement this processing process. It reads the specified file in the parameter and then returns its content as a string.
<%
'Pass the file name to the Function
Function getfilecontents (strincludefile)
Dim objfso
Dim objtext
Dim strpage
'Initialize FileSystemObject object
Set objfso = server. Createobject ("scripting. FileSystemObject ")
'Open the file and pass it to the textstream object (objtext ). Server Object
The 'mapath function is used to obtain the physical path of a file.
Set objtext = objfso. opentextfile (server. mappath (strincludefile ))
'Read and return the file content 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, read the main page (that is, the template file that contains the page layout and all static content) and save it to the string variable. Then, read the contained file and save it as a string variable. Finally, insert the variable containing the file content into the content on the home page.
<B> 2. Application Instance </B>
First, let's take a look at the template file. The following code contains an HTML comment "<! -- Include file here --> ", we will replace this HTML comment with the content of the contained file.
<HTML>
<Body>
<H2> welcome to visit! </H2>
<Table width = "500" border = "1">
<Tr>
<TD>
<! -- Include file here -->
</TD>
</Tr>
</Table>
</Body>
</Html>
Next, let's take a look at the several contained files used in this example. The first include file is the default include file. By default, a contained file is a form that allows you to select one of the other three contained files. Note that this form does not specify the "Action" attribute, which means that the form will be submitted to itself (I .e., reopen dynamicin3.asp ).
<! -- Begin default include -->
<Form method = "Post">
<H3> select a file you want to open <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 -->
For simple calculation, the content of the other three files is very simple:
<! -- Begin include file #1 -->
<H2 style = "color: Red"> file #1 </H2>
<Br>
<A href = "dynamicinc3.asp"> return to the default page </a>
<! -- End include file #1 -->
<! -- Begin include file #2 -->
<H2 style = "color: Green"> file #2 </H2>
<Br>
<A href = "dynamicinc3.asp"> return to the default page </a>
<! -- End include file #2 -->
<! -- Begin include file #3 -->
<H2 style = "color: Blue"> file #3 </H2>
<Br>
<A href = "dynamicinc3.asp"> return to the default page </a>
<! -- End include file #3 -->
The following is the code for the dynamicinc3.asp page and its description.
<%
'-------------------------------------------------------------
'Getfilecontents function put at the beginning of ASP file
'-------------------------------------------------------------
'Declare variables that store the home page and file content
Dim strmain, strinclude
'Read the content on the home page and save it to the strmain variable.
Strmain = getfilecontents ("maintemplate. Inc ")
'Check whether the cbofile selection box is selected. If so, read the required 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 include file into the variable strinclude
'Use the replace function to insert it into the strmain file.
Strmain = Replace (strmain, "<! -- Include file here --> ", strinclude)
'Send the result to the client
Response. Write strmain
%>
In this example, the system can run normally and achieve the goal of dynamically including files. Instead of using the include command, FileSystemObject is used instead.
Run this example, right-click the page and view the source file. You can find that the default include file is inserted in the page. Then, select an included file from the selection list. After submitting the form to reload the page, you can view the source file on the page. You can see that the selected included file is inserted.
This technology is useful when we want to separate the layout of the page and its content. We can create templates that describe the website layout, create website content in the form of files, and use ASP to easily combine the two!