Include files
You can place all of your commonly used code in one simple file and
include it in any number of other ASP files you want. So when you need
to update one piece of code that is used throughout your application
you can simply update it in one file ie the include file and every
other page that uses the include file will automatically be updated.
The include file can come in handy for such things as functions,
headers and footers and the connection string. Believe me it can save
alot of maintenance and updating time.
Below is an example of how to use an include file:
<html>
<head>
<title>Using include files</title>
</head>
<body>
<!--#include file="myfile.asp"-->
<!--#include virtual="/includes/myotherfile.asp"-->
</body>
</html>
There are two way of using an include file.
1. Virtual, using the 'virtual' keyword
2. Relative, using the 'file' keyword
Virtual:
In using the virtual keyword it assumes that you are starting from
the webserver's root directory. You are not concerned with the relative
path from the file that you will be including it in, but exactly where
it is located starting from the root of the website.
example 1
<!--#include virtual="/myfile.asp" -->
Using the code above assumes that the file 'myfile.asp' is located
in the root directory.
example 2
When building my sites I sometimes put all my include files in an include
folder called 'includes'. If I wanted to call a file called 'myfile.asp'
in the includes folder with the virtual keyword then it would look like
this
<!--#include virtual="/includes/myfile.asp"
-->
It doesn't matter what the directory location
of the page calling the include file is.
Relative:
The file keyword indicates a relative path so if the include file was
in the folder 'test' then the relative path from the current folder
might be
<!--#include file="../test/yourfilename.asp"
-->
or if the file was in the same folder then it would simply be
<!--#include file="yourfilename.asp"
-->
So just to note that with virtual you are not concerned with the path
relative to the file from which you will call the include file but its
exact absolute position starting from the root directory of the website.
Whereas with the file keyword you are concerned with the relative address
from the file that you will be calling the include file from.
Remember to place this code outside of the ASP script blocks, ie not
within ASP delimiters <% %>
Include files can end with the "inc" extension. However, for security
reason i prefer using the "ASP" extension. Functions written in files
using the "inc" extension can be opened in a browser window and viewed.
Functions written in files using an "ASP" extension are not displayed
to the viewer, thus keeping the source code hidden from hackers.
You may want to read how I built this site using includes here.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|