How do I dynamically include files?
One of the greatest obstacles faced by ASP developers is the ability to dynamically include files. Since #include directives are processed BEFORE the ASP code, it is impossible to use if/else logic to include files.
Or is it?
Depending on what you are doing within your include files, and how many you are including, it IS possible to use if/then logic to make use of includes. While it is not feasible for all situations, and it is often an inefficient solution, it can occasionally be quite a handy workaround.
Let's start with two sample HTML files, 1.htm and 2.htm. For the sake of simplicity, they contain very simple code:
Now, let's set up some conditional includes! For this example, we'll assume you want to include the file 2.htm if your page is passed a parameter of 2, otherwise include 1.htm. Here is an example of how you could accomplish this task:
Now try accessing the page in these three ways, and experiment with the results:
Of course you can perform this kind of include logic based on various conditions, such as the date, the time, or the user's browser.
Please note that in the above example, if you are still using IIS 4.0, BOTH include files will be processed (IIS 5.0 is much smarter about this). So, the more options you have, the less efficient this solution will be. When the number of possible includes start getting a bit high, and the code in the include files is pure HTML (no ASP logic), you could try something like this:
(If the file has ASP logic, it will be displayed as plain text, because it won't be available to the ASP engine early enough.)
Another option, in IIS 5.0 and up, is to use "dynamic includes" via Server.Execute. Using the above scenario, here is an example:
Please see Article #2006 for some of the common problems people have with the the Server.Execute and Server.Transfer methods.
Related ArticlesCan I include a file in both server-side and client-side script?
Can I read / write a user's file without a prompt?
Can I rename a file using FileSystemObject?
Could I get some help working with files using FileSystemObject?
How do I avoid 'the red x' when an image is missing?
How do I change the modified time of a file?
How do I create / manipulate images from ASP?
How do I find the owner, author, and other properties of a file?
How do I get a list of a folder's subfolders?
How do I get the name of the current URL / page?
How do I prevent people from 'leeching' my CSS or JS files?
How do I prevent people from 'leeching' my images?
How do I retrieve a random file from a given folder?
How do I send the correct filename with BinaryWrite?
How do I sort a list of files?
How do I use FileSystemObject to create a file on the client?
Why do I get 'Disk not ready' errors with FileSystemObject?
Why do I get 'Invalid procedure call or argument'?
Why do I get 'Path not found' errors with Scripting.FileSystemObject?
Why do I get 'Permission Denied' errors with FileSystemObject?
Why do I get 800A0034 errors?
Why do I get 800A003E / Input past end of file errors?
Why do I get 800A0BBA errors?
Why do I get 800A0BBC errors?
Why do I get an 'Invalid Path Character' error?
Why do I get permissions errors after upgrading to Windows XP?
Why does FileSystemObject hang all of a sudden?
Why is 'the operation completed successfully' an error message?