#includes, Part 3
#includes, Part 1#includes, Part 2
Dynamic Includes: What are they?
Wouldn't it be neat to be able to write an ASP Script that included a file based on some parameter
passed in? For example, you might have fifty text files, with each text file containing a different
state's motto. It would be neat to have the user enter his state's abbreviation in a form, and then
be taken to an ASP page that used an #include to load in the correct motto. For example:
<%
|
Well, I'd agree that being able to do that would be neat, but the fact of the matter remains that we can't do that! Pure dynamic includes are impossible using just ASP. That is a fact. I hope I haven't ruined your day.
Dynamic Includes: Why the Hell Can't I Use a Dynamic Include?
Well, if you'll recall the gritty details presented in the previous article,
files are included before the ASP gets a chance to be parsed. This allows us to include files that
contain ASP script, something we couldn't do if the ASP script was parsed before the includes were performed.
So, since the ASP script is not processed when the includes are performed, we can't expect IIS to know what
<%=State%> equals. You'll get an error, telling you it can't find the file
/mottos/<%=State%>. Don't give up just yet, there are some workarounds.
Dynamic Includes: Workarounds
If you need to include files that have ASP script in them, you are going to have to use a case statement to
execute the correct ASP code. For example, let's say you know you will include one of three files, but
you don't know what file exactly, since it is determined on some variable named pickFile.
If pickFile equals 1, you want to include 1.asp; if it equals 2, include
2.asp, and if it equals 3, you want to include 3.asp. You could do the following:
<%
|
All three files, 1.asp, 2.asp, and 3.asp, would be loaded through SSI, but the code that was executed
would depend on the value of pickFile.
If you want to include files that have no ASP script, you can use the FileSystemObject to dynamically generate the filename. You can then use FSO to open the file and dump the contents. Check out Charles Carroll's Dynamic Include Lesson or Michael Qualls's Dynamic Include Files article to learn more about this technique.
Dynamic Include Files with Server.Execute |
|---|
With ASP 3.0 you can use the Server.Execute method to kind of fake a
dynamic include. With Server.Execute, the ASP page called is loaded, run, and
then unloaded. This is great if you have some procedural ASP code you want to execute, but
oftentimes developers fill #include files with commonly used functions.
Assume that A.asp contains the code: Server.Execute("B.asp"). Because
of the nature of Server.Execute, A.asp can't call any functions
declared in B.asp! For that reason, Server.Executes are rarely useful
for dynamic includes, in my opinion.
For more information on |
Conclusion:
So, what have we learned today? We've learned how to include files, using #include file and
#include virtual. We've learned about the gritty details of include files, about the
advantages of include files, and some recommended conventions. Also, we learned about dynamic includes,
and why they're not possible. I hope you've had fun. If you have any questions or comments on this article
or on 4Guys or the ASP Messageboard, be sure to send us feedback.
Happy Programming!
#includes, Part 1#includes, Part 2




