
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.
Содержание COLDFUSION 5-DEVELOPING
Страница 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Страница 58: ...38 Chapter 3 Querying a Database ...
Страница 134: ...114 Chapter 7 Updating Your Database ...
Страница 210: ...190 Chapter 10 Reusing Code ...
Страница 232: ...212 Chapter 11 Preventing and Handling Errors ...
Страница 238: ...218 Chapter 12 Using the Application Framework ...
Страница 262: ...242 Chapter 12 Using the Application Framework ...
Страница 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Страница 320: ...300 Chapter 15 Indexing and Searching Data ...
Страница 336: ...316 Chapter 16 Sending and Receiving E mail ...
Страница 374: ...354 Chapter 18 Interacting with Remote Servers ...