A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.
It's fast and free.
processing...Retrieve Sign In
Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.
processing...Welcome to Webmonkey
- edit articles
- add to the code library
- design and write a tutorial
- comment on any Webmonkey article
Sign In Information Sent
Use includes with CFML
/skill level/
/viewed/
The concept of using "includes" when programming is not a new one, and just about every programming language has this feature in one form or another. However, if you are new to programming, the basic idea of an "include" is to take a piece of code that you will use over and over, and put it in one simple place. That way, whenever you needed to make a change to that code, you'd only have to update ONE page, instead of updating every page on your site that you used that code on.
In the following example, I'll be demonstrating how to use includes with the CFML development language. CFML has been around for almost 10 years now, with commercial servers available from Adobe and NewAtlanta, and is widly regarded as one of the easiest web development languages to learn because of it's HTML-like tag-based structure. A free CFML server is available from the Open BlueDragon project, which is licensed under the GPLv3 and can be used freely on any web server.
Some good examples of code you would use over and over on a site would be your site header, or your site menu, or your site footer. All are relatively basic aspects of your site that usually don't change much as a visitor moves from page to page throughout your site. This makes those aspects of your site perfect to use includes with.
The following is an example of some web site code we might want to break down into re-usable pieces:
index.cfm
<HTML>
<head>
<title>My page</title>
</head>
<body bgcolor="#CCCCCC">
My Site |
|
|
- <a href="index.cfm">Home</a> |
Super-cool content goes here. |
</body> </HTML>
As a visitors moves from page to page on my site, the header, the menu, and the footer are not going to change. Those are going to say the same no matter what page the user is on while visiting my site. This is the perfect opportunity to use includes.
The first thing we need to do is identify what part of our code that we're going to use repeatedly. In the case above, those pieces would be the header, the menu, and the footer, which can be broken down into the following pieces of code:
header.cfm
<HTML>
<head>
<title>My page</title>
</head>
<body bgcolor="#CCCCCC">
