Including Files In Asp
Posted on 15 Mar 2000
by Adrian Roselli (aardvark)
Rated 4.21 (Ratings: 7)
- More articles in Code
'%>'script tags, but this isn't the case with Active Server Pages. Instead, you'll notice the include directive is reminiscent of Apache, or other web servers that allow server-side includes. As such, there are some rules to including files and keeping them from breaking your script that you have to keep in mind. So let's jump right in...To include files in ASP pages:
<!--#include virtual file ="filename"-->The
'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, MS docs will recommend giving included files an
.incextension to distinguish them from other types of files. I prefer to create a directory that contains all the files, and give them all an
.aspextension. If someone finds that directory and/or file name (which can happen if your ASP scripts break and it shows the offending code), then the ASP parser will execute the script within. By default, the server will treat an
.incfile as plain text and serve it as such. Often my include files are functions or navigation, so the result is a blank screen or an odd HTML page if I use an
.aspextension. This prevents a user from seeing my code (which may include database connection information), and while it may generate an error, this is preferrable to showing all my script as plain text.
Using the 'Virtual' KeywordUse 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 '
/footer,' the following line would insert the contents of '
footer.inc' into the script containing the line:
<!--#include virtual ="/footer/footer.inc"-->
Using the 'File' KeywordUse 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 '
headers', and the file '
header.inc' is in '
root\headers', the following line would insert
'header.inc'into your script:
<!--#include file ="headers/header.inc"-->Note that the path to the included file, '
headers/header.inc,' is relative to the including file; if the script containing this
#includestatement is not in the directory '
/root', 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.
- You can have one include file include another. A file cannot include the file including it, nor can it include itself. That would be bad.
- The include file must not have open control structures, nor can it be used to close control structures. You can't start an '
if' statement in your main page and have the corresponding '
end if' in the included file. The include file has to be able to stand on its own.
- The ASP parser includes a file before executing any script commands. This means you can't put a variable in the filename and include files dynamically based on that variable's value. You can, however, load the include into a SUB and call the SUB only if certain criteria are met. For instance:
<% IF foo = "bar" THEN %> <!--#include file ="include/bar.inc"--> <% ELSE %> <!--#include file ="include/foo.inc"--> <% END IF %>Will still load both include files into the ASP script, as opposed to only loading one of them. However, a cleaner way to do this might be:
<% IF foo = "bar" THEN BAR ELSE FOO END IF %>
<% SUB FOO %><!--#include file ="include/foo.inc"--> <% END SUB %>
<% SUB BAR %><!--#include file ="include/bar.inc"--> <% END SUB %>
- You must close your script tags before including a file. For instance:
<% SUB FOOTER %> <!--#include file ="footers/footer.inc"--> <% END SUB %>Will work. However,
<% SUB FOOTER <!--#include file ="footers/footer.inc"--> END SUB %>Will not.