Ways To Reuse Code; Reusing Common Code With Cfinclude - MACROMEDIA COLDFUSION 5-DEVELOPING Develop Manual

Table of Contents

Advertisement

172

Ways to Reuse Code

ColdFusion provides several different ways to reuse code. These ways include the
following techniques:
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
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
To reference code in a calling page:
1
2
3
4
5
If you are using ColdFusion Studio, you can write code snippets, which you can
copy into pages.
You use the
cfinclude
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.
tag to automatically include an existing file in the current page.
cfinclude
syntax, see the CFML Reference.
cfinclude
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.)
Open the file askemp.cfm in ColdFusion Studio.
Include header.cfm in this page by adding the following line just below the
tag:
<body>
<cfinclude template="header.cfm">
Save the page.
Open getemp.cfm in ColdFusion Studio.
tag to include a ColdFusion page in another page.
Chapter 10 Reusing Code

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 5

Table of Contents