Keeping your Code Modular with INCLUDE

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!

Introduction to ASP Ebook

Download this ebook to get everything you need to know about learning ASP - from a step by step tutorial to function lists, sample code, common errors and solutions, and much more! 101 pages.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Lisa Shea. All rights reserved.
This content was written by Lisa Shea. If you wish to use this content in any manner, you need written permission. Contact Lisa Shea for details.