72
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
2.
Include
<CFINCLUDE TEMPLATE="logo.cfm">
3.
Save the page.
4.
Open
5.
Include
<CFINCLUDE TEMPLATE="Logo.cfm">
6.
View
getemp.cfm
The logo should appear on both pages.
Note
in Studio.
askemp.cfm
in this page:
logo.cfm
in Studio.
getemp.cfm
file in this page:
logo.cfm
in a browser, then submit the form so that you display
askemp.cfm
.
The file
must be in the same directory where you saved
logo.cfm
and
askemp.cfm
getemp.cfm
has a mapping defined in ColdFusion Administrator, or move it to the
appropriate directory.
Developing Web Applications with ColdFusion
. If it isn't, make sure it is in a directory that
Need help?
Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?