Including Files

Contact Us or call 1-877-932-8228
Including Files

Including Files

Including files in ColdFusion is made simple with the <cfinclude> tag, which takes a single attribute: template. The template attribute points to the file to include. ColdFusion first looks for the included file relative to the current directory. If it cannot find it, it then looks in directories mapped in ColdFusion Administrator.

<cfinclude template="path_to_file">

Note that a ColdFusion tag cannot be opened in a calling file and then closed in an included file or vice versa. The ColdFusion code in the included file must be syntactically valid on its own.

A Note on Security

If included files are under the web root, they can be accessed just as any other file can. If they have an extension such as .inc then the browser may display them as plain text. With other extensions, the browser may attempt to download the file. If the included file is a ColdFusion file and a user navigates to it, the server will try to process the file and may return errors. As a precaution, you may want to place your included files in a directory above or outside of the web root. This will prevent users from accessing the files directly.

Code Sample:

ReusingCode/Demos/index.cfm
<html>
<head>
<title>Runners Home&trade;</title>
<link href="Styles/Main.css" rel="stylesheet">
</head>

<body>
<cfinclude template="Includes/NavBar.cfm">

<div id="greeting">
	<cfoutput>The time is #TimeFormat(Now(),"h:mm tt")# on #DateFormat(Now(), "mmmm d, yyyy")#.</cfoutput>
</div>

<table align="center" cellpadding="10" cellspacing="0" width="100%" height="100%" id="hometable">
---- C O D E   O M I T T E D ----
</table>
<cfinclude template="Includes/Footer.cfm">

</body>
</html>

The above code is relatively straightforward. It contains to included files: Includes/NavBar.cfm and Includes/Footer.cfm.

Next