'%>'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.
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"-->
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.
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.
<% 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 %>
<% SUB FOOTER %> <!--#include file ="footers/footer.inc"--> <% END SUB %>Will work. However,
<% SUB FOOTER <!--#include file ="footers/footer.inc"--> END SUB %>Will not.