Saturday, July 27, 2024

Using Include Files

Server Side Includes (SSI) are a powerful tool for code re-use and to make site maintenance easier. On this page alone, there are 2 includes, one of which includes other files in it. This can make it much easier for the developer to maintain a consistent site. One of the most popular uses for SSIs is to create uniform headers and footers, often with menu items. On this site, I have both a standard header and a standard footer, which are referenced from one include file, called articleformat.asp.

Whenever possible, you should avoid simply including files inline in your ASP code. The reason for this is that if any variables are defined or changed in the included file, your ASP page may bomb due to a duplicate declaration or behave unexpectedly with the new variable’s value. By using functions for all of your include files, you can place all of your includes at the top of your ASP file, where you can quickly see which files any given page depends on. Variables within the function are of local scope, so there is no need to use hacks like making up strange variable names in order to avoid name conflicts. Wherever you want to use the include file, you simply add a line of code invoking the function. For many of my library functions, I name the include file the same as the function to make it easy to tell which functions are in which files and what they’re called.

Click here now for the code.

In summary, anything that you will use on more than one page is a good candidate for use as an include file. Menus should ALWAYS be include files if they are not in a separate frame (which is a whole different subject). Also, it is a good idea to declare all of the variables in your include files, so that all pages that call it using OPTION EXPLICIT will work.

Microsoft has acknowledged problems with the “*.inc” naming convention of include files. It is possible for users to access the source code of these files, depending on what version of IIS you are running. The fix is to name all include files with the “*.asp” extension. A common naming convention is to append “inc” to the end of the filename before the extension. So for example, the standard include file with all of ADO’s constant names is “adovbs.inc”. This would be renamed to “adovbsinc.asp”. Another common practice is to throw all of the include files for a web site into a “/include/” subdirectory off of the main page. This makes accessing them simple regardless of what directory you are accessing them from. By doing this, you can easily reference all of your include files using VIRTUAL paths, making it easier to maintain your site when you move ASP files from one directory to another (which would break FILE paths if the depth of the ASP file in the directory structure changed). One last note — any server side Sub or Function that you might use on more than one page should go into an include file.

By Steven Smith
http://www.aspalliance.com/

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Advantages of local domestic helper.