Keeping your Code Modular with INCLUDE

Do you find yourself doing the same thing over and over in your code – always having the same footer, always using the same title area? Use the INCLUDE statement so you only do it once!

Most websites strive for consistency. You want the pages to all look alike, with the same title area, the same footer, maybe the same sidebars. While this makes the pages look nice, it also means that changes can be a royal pain. If you want to change the footer, now you have to go into 200 or more pages and change it everywhere. Even if you use a search-and-replace utility, it can still be a pain.

To get around this, get yourself acquainted with the INCLUDE statement. With INCLUDE, you tell the webpage to “drop in” a block of code from another file. This code can be raw HTML text, ASP code, or just about anything else you’d normally put into your ASP file. The included code can even refer to variables you’re using in your main webpage.

For example, say you have a standard footer you use. This footer gives the name of the page you’re on, a link back to your main homepage, and an email link for contact information. Normally, you might code this into every single page since the page name changes. There’s no reason to, though.

First, set up a variable on each page that has identifying information about that page. You could have

PageName = “Press Releases”

for example, if this was your Press Releases page you were working on.

Now, down where you want the footer to go, put in the code

<!–#INCLUDE FILE=”Footer.asp” –>

This tells the webpage to ‘suck in’ whatever it finds in Footer.asp, and put it right in this spot when it’s drawing the page. The user doesn’t see that include statement. They see the actual contents of that file, right in that spot.

So far so good? Now to make that Footer.asp file.

Create a new file, named Footer.asp. In this file, you’re going to put whatever you want to appear in the footer. Let’s say we want a horizontal line, then the three pieces of information – what page you’re currently on, a link back to the site homepage, and an email link. You could do something like this:

<%=PageName%> |
<A HREF="index.asp"> |
<A HREF="">

Now when you put this Footer.asp page and your regular webpages on your site, the two will work together to give you consistant, customized footer pages. If you ever want to update the footer, you just change the one Footer.asp, and voila! It changes everywhere!

Now, I’ll note that I used to use includes everywhere, for all sorts of reasons. But I found that all the “building” process of doing all those file access calls every time a page drew was taking time.

So now I’m more judicious.

I do use includes for things like database usernames and passwords. But if the footer area stays exactly the same all year long and only changes on December 31st, I’ll hard code the footer everywhere. Once a year it takes me maybe 2-3 minutes to run a search-and-replace and update that spot. But then for the entire rest of the year it means the page runs just a bit faster, since it doesn’t have to do an extra include there.

This page is lesson ten in my free ASP Classic Course. Click below to enjoy the full course from start to finish!

Free Online ASP Course