User Guide
172 Chapter 10 Reusing Code
Ways to Reuse Code
ColdFusion provides several different ways to reuse code. These ways include the
following techniques:
• If you are using ColdFusion Studio, you can write code snippets, which you can
copy into pages.
• You use the
cfinclude tag to include a ColdFusion page in another page.
Included pages behave just as though you typed the included code directly into
the calling page.
• You can create custom CFML tags. Unlike included pages, these custom tags act
as other tags do. You pass parameters to the custom tags from the calling page
and the custom tag pages have their own local Variables scope.
The following sections describe these techniques in more detail.
Reusing Common Code with cfinclude
Often, you 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 page that calls the included page is sometimes referred to as the calling page.
Each time the calling page is requested, the included page’s file contents are added
in that page for processing.
For
cfinclude syntax, see the CFML Reference.
To reference code in a calling page:
1 Create a file header.fm that displays your company’s logo. Your page could
consist of just the following lines or it could include many lines to define an
entire header.
<img src="mylogo.gif">
<br>
(Of course, for this code to work you must also put your company’s logo as a gif
file in the same directory as header.cfm.)
2 Open the file askemp.cfm in ColdFusion Studio.
3 Include header.cfm in this page by adding the following line just below the
<body> tag:
<cfinclude template="header.cfm">
4 Save the page.
5 Open getemp.cfm in ColdFusion Studio.