View Full Version : Using include with a query string?
dagaffer
09-15-2002, 02:27 PM
Hi,
I was trying to be lazy as my webpage has a large template to it, i wanted to have the 1 default page & every other page would be used in an included file.... (does that make sense?!:confused: ... iv never been good at english)
So I had put into the template:
<%
Dim page
page = Request.QueryString("page")
%>
<!--#include file="<%=page%>".txt-->
but that just simply comes up with
The include file '<%=page%>' was not found.
How would i get the object in the query string to be used in the include file?
dfrancis
09-15-2002, 03:30 PM
This only works with IIS5 because previous versions process the ASP in time. :o
Mysite.com/default.asp?page=subfolder/page2.asp
From the looks of your error however, I would suggest that your host (PWS?) does not have IIS5 and in this scenerio, you have to use either a select case or FSO to dynamically include a page based upon the QS variable. I used to have some articles about this but after pounding my head against the wall for weeks on this... I think I deleted the articles out of anger.
This is one from the MSDN
Including FilesServer-side include directives give you a way to insert the content of another file into a file before the Web server processes it. ASP implements only the #include directive of this mechanism. To insert a file into an .asp file,
Use the following syntax:
<!--#include virtual | file ="filename"-->
The virtual and file keywords indicate the type of path you are using to include the file, and filename is the path and file name of the file you want to include.
Included files do not require a special file name extension; however, it is considered good programming practice to give included files an .inc extension to distinguish them from other types of files.
Using the Virtual Keyword
Use the virtual keyword to indicate a path beginning with a virtual directory. For example, if a file named Footer.inc resides in a virtual directory named /Myapp, the following line would insert the contents of Footer.inc into the file containing the line:
<!--#include virtual ="/myapp/footer.inc"-->
Using the File Keyword
Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file. For example, if you have a file in the directory Myapp, and the file Header1.inc is in Myapp\Headers, the following line would insert Header1.inc in your file:
<!--#include file ="headers/header1.inc"-->
Note that the path to the included file, Headers/header1.inc, is relative to the including file; if the script containing this #include statement is not in the directory /Myapp, the statement would not work.
You can also use the file keyword with ../ syntax to include a file from a parent, or higher-level, directory if the Enable Parent Paths option is selected in Internet Service Manager.
Location of Included Files
Included files can reside within a directory in your Web site or outside of your Web site. Generally, you should keep included files within Web site directories. If an included file resides within a directory in your Web site, changes to the included file appear the next time a browser requests the including file. If, however, the included file resides outside your Web site, changes will not appear until the ASP application is restarted or until the Web server is restarted. ASP detects changes to any including file that is contained in the application name space (under an application starting point directory).
Including Files: Tips and Cautions
An included file can, in turn, include other files. An .asp file can also include the same file more than once, provided that the #include directives do not cause a loop. For example, if the file First.asp includes the file Second.inc, Second.inc must not in turn include First.asp. Nor can a file include itself. ASP detects such loop or nesting errors, generates an error message, and stops processing the requested .asp file.
ASP includes files before executing script commands. Therefore, you cannot use a script command to build the name of an included file. For example, the following script would not open the file Header1.inc because ASP attempts to execute the #include directive before it assigns a file name to the variable name.
<!-- This script will fail -->
<% name=(header1 & ".inc") %>
<!--#include file="<%= name %>"-->
Scripts commands and procedures must be entirely contained within the script delimiters <% and %>, the HTML tags <SCRIPT> and </SCRIPT>, or the HTML tags <OBJECT> and </OBJECT>. That is, you cannot open a script delimiter in an including .asp file, then close the delimiter in an included file; the script or script command must be a complete unit. For example, the following script would not work:
<!-- This script will fail -->
<%
For i = 1 To n
statements in main file
<!--#include file="header1.inc" -->
Next
%>
The following script would work:
<%
For i = 1 to n
statements in main file
%>
<!--#include file="header1.inc" -->
<% Next %>
dagaffer
09-15-2002, 03:39 PM
So other words, I can't do it with my server?
Thanks for the help anyway, I'll now go off & find something else to do...
glenngv
09-16-2002, 07:05 AM
<%
Dim page
page = Request.QueryString("page")
if page="yourIncludeFileHere" then
%>
<!--#include file="yourIncludeFileHere.txt"-->
<%
end if
%>
dfrancis
09-16-2002, 03:31 PM
<%
Dim page
page = Request.QueryString("page")
if page="yourIncludeFileHere" then
%>
<!--#include file="yourIncludeFileHere.txt"-->
<%
end if
%>
Yes, that will work... but I think the question was how to allow for a variable in the include file line. Using the above method, one would have to define all includes using the if/else statement. This works if you have a few pages (select case works a little better) but for a truly dynamic include, (for ASP anyway) one would need IIS5.
Another way some people have accomplished is using the File System Object... the MSDN has an article on it in periodicals somewhere. ("Web Men Talking")
dfrancis
Morgoth
11-27-2002, 12:53 AM
OMG!
<%
Dim page
page = Request.QueryString("page")
if page="yourIncludeFileHere" then
%>
<!--#include file="yourIncludeFileHere.txt"-->
<%
end if
%>
This works on IIS5!
I think I must of been IIS4 when I tried this long ago... It didn't work for me because of IIS4
But it seems to work now..
I thought it wouldn't work because "<!--#include file="yourIncludeFileHere.txt"-->" ran before the ASP language did...
Hum.... This is odd.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.