Including Other Templates

ColdFusion allows you to embed references to other ColdFusion templates, HTML documents, and plain-text files in your ColdFusion applications via the <cfinclude> tag. cfinclude is the ColdFusion equivalent of Server Side Includes (SSI). cfinclude takes a single attribute, template, which specifies a logical path to the file to be included. The logical path must be either a virtual directory or a directory that has been explicitly mapped in the ColdFusion Administrator:

<cfinclude template="MyIncludedFile.cfm">

Or:

<cfinclude template="/MyDirectory/MyIncludedFile.txt">

Including files allows you to use repetitive code without having to cut and paste it into your template every time you want to use it. A good example of this is header and footer files that contain things such as site navigation, legal notices, and copyright information. By including header and footer files with each of your templates, you can make changes to the header or footer once and have that change instantly available to every template that includes the files. To understand how this works, consider the following ColdFusion template that includes both a header and footer file:

<!--- Set the title for the page --->
<cfset Title="My Page">
   
<!--- Include the header for the page --->
<cfinclude template="_header.cfm">
   
<h2>Hello World!</h2>
I'm just some regular text.
   
<!--- Include the footer --->
<cfinclude template="_footer.cfm">

You can save this template under any name you want. For ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.