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.

Note To reference code in a calling page:
  1. Open the file askemp.cfm in Studio.
  2. Include logo.cfm in this page:
    <CFINCLUDE TEMPLATE="logo.cfm">
    
  3. Save the page.
  4. Open getemp.cfm in Studio.
  5. Include logo.cfm file in this page:
    <CFINCLUDE TEMPLATE="Logo.cfm">
    
  6. View askemp.cfm in a browser, then submit the form so that you display getemp.cfm.

    The logo should appear on both pages.

Note The file logo.cfm must be in the same directory where you saved askemp.cfm and getemp.cfm. If it isn't, make sure it is in a directory that has a mapping defined in ColdFusion Administrator, or move it to the appropriate directory.