FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Simplify your documents with CFINCLUDES

By: Lawrence Cramer

Page 1 of 1

Set for printing

Site maintenance can be a real bear! A simple task such as changing an address can take hours, especially if the address is repeated on every page of a large site. You could always try a global search and replace, but that can be tricky and it’s a real good way to cause global havoc to your site if something goes wrong. So to be safe you end up having to back up the entire site. Now you've got multiple copies of the site floating around... Not good!

A “Dynamic” Solution!

With sites using dynamic server languages, there is a better way. This tutorial will be using ColdFusion, but most of the principles apply to other languages as well. There are several methods for reusing code in the various server platforms but Includes are the most pervasive and the one you will probably use most often. In CF there is a simple tag that calls the include file.

Includes vs DW Templates

Why use includes? Why not just use the DWMX Template feature? For non dynamic or "static" sites, DreamweaverMX's Template feature is a good way to achieve the "edit once, apply everywhere" ideal. For this reason, DW Templates is a very popular feature - but there is a shortcoming. Although DreamweaverMX will take care of making the site-wide changes for you, to apply the changes to your live site you still have to upload EVERY FILE affected by the change. On large sites with hundreds or even thousands of files, this can be a major headache in itself. With includes you make the change to one file, then you upload that one file to the server and the change instantly takes effect site-wide on every file that calls the updated include file. A much faster and more elegant solution.

How Includes Work

If the file you are calling is named “header.cfm” and it is in the same directory as the file calling it, the include tag will look like this…

<cfinclude template="header.cfm">

There are a few important rules to keep in mind when writing the code for the include files.

  1. Do not use any <body>, <head>, or <!DOCTYPE> tags.
    The reason for this is the include file is merged into the code of the calling file. To the browser it appears as though it is simply part of the file. If the tags mentioned above are in your include files, they will appear to be duplicated to the browser and will cause errors.
  2. Do not use any CSS links.
    For the same reason, this would cause the browser to see two CSS links. This would at least cause the browser not to render the CSS and in some browsers make the page completely incompatible.
  3. Do not use <meta> tags in most includes.
    Meta tags should be placed in between the <head> tags of a document. Your include file's code will be placed where you place the <cfinclude> or SSI tag. So if the tag is going to be placed between the <body> tags you would not want to have <meta> tags in the include file's code.

**Now a point to consider: If the include is placed between the <head> tags, you could have <meta> tags in your code. In fact, this is a great way to place and control your meta tags. If you develop a "meta include" and place it between the <head> tags, you can easily modify your meta tags later.

  1. Be careful about mapping.
    The mapping to links and image files must be relative to the file calling the include. So the include needs to be in the same folder as the file calling it. If it is in a separate folder, the mapping needs to be either absolute - that is, contain the full site address ( for example - 'http://www.yourdomain.com/folder/file.cfm) to all links and assets, or must be relative to the document calling it, regardless of where the include file is being stored.

Here is a good example of how to use includes:

Include usage example

Summary

As you can see, the only code that is unique to the page is the table or layer structure that holds the content, includes and the body content itself. Everything else is delivered via include files. Need to change a phone number in the footer? No problem! Do it once, upload the file and your 800 page site is updated. How's that for efficiency?

Download and open the example files and see how much using includes can simplify your code, and your life!

Page 1 of 1 1


download
Download Support Files


Keywords
ColdFusion, SSI, CF Includes, Site Maintenance