Tuesday, September 12, 2006

ASP Includes Explained

Include files are external files that you can easily add to any page:
It's important to note that include files are processed and inserted BEFORE scripts on the page are run. Included files can contain functions, sub-routines, navigational elements, or just about anything else. Include files are at the heart of making efficient ASP based sites, as they allow you to put you code into easily re-usable chunks.

There are two forms of the include statement: Virtual and File:

By using Virtual, you can include any file no matter what its location because Virtual starts as the root directory of the site. In contrast, the File statement starts in the CURRENT directory (the same directory as the file containing the Include).

Example #1
could include a file from the AAA directory, even if the page with this statement is in the /BBB/ folder.

Example #2:
would fail when used in any file not located in the AAA directory.

Example #3:
will succeed from any directory on the same level as the AAA directory. It's instructing the include to go up one directory and then into the AAA directory to get the file. However, if the page that contains the include is moved to a different level in the directory structure, the Include statement would fail.

I almost always use the Virtual format. That way I know I'm starting at the site root. Even if I move the page containing the include to a different directory, it will still work.

No comments: