Reusing Code

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 it from page to page, ColdFusion allows you to reference the code stored in one file in many pages. This way you can modify one file and recognize the changes throughout an entire application.

Use the CFINCLUDE tag to automatically include an existing file in the current page.


<CFINCLUDE Template="File.cfm">
Note Note:Refer to the CFML Language Reference for ColdFusion Express for CFINCLUDE syntax.

Usage example

In this example, the navigation toolbar is included (not copied) at the top of the calling page, EmpList.cfm:


During the next procedure, use CFINCLUDE on your EmpList.cfm and FormAction.cfm pages to reuse the application's navigation code.

Note To reference code in a calling page:
  1. Open EmpList.cfm in HomeSite.
  2. Delete the existing toolbar code that begins with this:
  3. <!--- begin application toolbar imported from toolbar.cfm--->

    And ends with this:

    <!--- end application toolbar --->
  4. Include Toolbar.cfm file in this page:
  5. <CFINCLUDE TEMPLATE="ToolBar.cfm">
  6. Save the page.
  7. View EmpList.cfm in a browser.
  8. The toolbar should still appear at the top of the page.
  9. Return to HomeSite.
  10. Open FormPage.cfm.
  11. Save it as SearchForm.cfm.
  12. You will modify this form during the next chapter's procedures to create a search form.

  13. Add the CFINCLUDE tag directly below the opening BODY tag.
  14. Save the page.
  15. View EmpList.cfm in a browser and navigate to SearchForm.cfm by clicking SEARCH.
  16. The toolbar should now be included.

  17. Click LIST to return to EmpList.cfm.

Click here to see what the Emplist.cfm looks like.

Click here to see what SearchForm.cfm looks like.

Click here to see the search form's code.

In the next chapter, you will code the form so that it really does search the database.

Move on in this chapter to review development considerations.