Main Page Content
Including Files In Asp
As more people use Active Server Pages, I've noticed a trend that newer users can't figure out how to include files into their scripts. Part of the problem is that they expect an include directive to exist in the
'<%'
and '%>'
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
'virtual'
and '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 .inc
extension to distinguish them from other types of files. I prefer to create a directory that contains all the files, and give them all an .asp
extension. 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 .inc
file 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 .asp
extension. 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' Keyword
Use 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' Keyword
Use 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 #include
statement 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.Other Notes:
- 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.