User Guide

72 Developing Web Applications with ColdFusion
Ways to Reuse Code
ColdFusion provides several different ways to reuse code. If you are using ColdFusion
Studio, you can write code snippets, which you can copy into templates. For more
information on writing code snippets, see Using ColdFusion Studio. You can include a
template within another template with the CFINCLUDE tag. In addition, you can
create custom tags in CFML. Unlike included templates, these custom tags act as other
tags do, allowing you to pass parameters to them. Included templates, on the other
hand, behave just as though you typed the included code directly into the calling page.
Reusing Common Code with CFINCLUDE
Often times, you’ll use some of the same elements in multiple pages; for example,
navigation, headers, and footer code.
Instead of copying and maintaining the same code from page to page, ColdFusion
allows you to store the code in one page and then refer to it in many pages. This way,
you can modify one file; the changes appear throughout an entire application.
Use the CFINCLUDE tag to automatically include an existing file in the current page.
The file to include is the template. The page that calls the template is also known as the
calling page. Each time the calling page is requested, the template’s file contents are
included in that page for processing.
Refer to the CFML Language Reference for CFINCLUDE syntax.
To reference code in a calling page:
1. Open the file askemp.cfm in Studio.
2. Include
logo.cfm in this page:
<CFINCLUDE TEMPLATE="logo.cfm">
3. Save the page.
4. Open
getemp.cfm in Studio.
5. Include
logo.cfm file in this page:
<CFINCLUDE TEMPLATE="Logo.cfm">
6. View askemp.cfm in a browser, then submit the form so that you display
getemp.cfm.
The logo should appear on both pages.
Note The file logo.cfm must be in the same directory where you saved
askemp.cfm and getemp.cfm. If it isn’t, make sure it is in a directory that
has a mapping defined in ColdFusion Administrator, or move it to the
appropriate directory.