Including Application Page Files (CFINCLUDE)  
 
 

As the applications you develop with ColdFusion become more complex, you will require ways to simplify the presentation of your CFML application page files and to reuse code. The CFINCLUDE tag helps to fulfill these requirements.

Essentially, CFINCLUDE inserts another application file into the current page. It's a handy way to reuse common code.

 
 
  Note  
 

You cannot use CFINCLUDE to cause a CFIF, CFLOOP, or CFOUTPUT tag block to be split across different application pages.

The CFINCLUDE tag has a TEMPLATE attribute that specifies an existing application page file to process and return to the client. This application page file is processed as if it is a part of the file into which it is included. For example, if you create a variable in an application page and use a CFINCLUDE, the included application page can reference the variable created in the main application page.

 
 
  Syntax  
 

The syntax of the CFINCLUDE tag is:

<CFINCLUDE TEMPLATE="FileName">

Here, FileName represents the relative path of the application page file to be included.

 
 
  Examples  
 

For example, one of the simplest applications of CFINCLUDE is for adding common header and footer code to your pages, following this model:

<CFINCLUDE TEMPLATE="header.cfm">
... Page contents
<CFINCLUDE TEMPLATE="footer.cfm">

To process the file index.cfm, which is up one directory level from the current application page:

<CFINCLUDE TEMPLATE="../index.cfm">
 
 
  Potential uses of the CFINCLUDE tag  
 
 
  • Your application might display the same CFTABLE repeatedly on different pages. Rather than copy and paste the code from page to page, you could create an application page for the table and include it wherever it is needed.
  • You could create application page files called "header.cfm" and "footer.cfm," which contain a standard set of HTML text and tags to include at the beginning and end of the pages in your application. You could then modify the look and feel of your entire site by changing just two application pages.
  • You might have a complicated set of nested CFIF/CFELSE statements that could be clarified if the code to be executed was hidden away in application pages rather than displayed inline.

Do not split code blocks across included pages. Be sure to keep beginning and ending tags together.



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.