How to programmatically include a header and footer file in ColdFusion?

coldfusion, coldfusion-10, html, include

Solution

Chris has a good suggestion. Actually if you look into the code of most frameworks you are going to see approaches similar to the one you have suggested. Here are a few more things to think about.

First, consider using Application.cfc instead of Application.cfm. Application.cfc has "onRequestStart()" and "onRequestEnd()" functions that serve as you might expect to fire at the beginning (header) and end (foooter) as well as an "onRequest()" function where you can put your content include. This seems more reasonable to me and is organized in the way you are looking for.

Second, the code above depends on the "cgi.path_info" - that's going to bring some security issues to the front of the line. This variable is created by CF but it's created by parsing out the URL - so it's possible someone might figure out how to get a file to be included OTHER than a legitimate web file. A common example would be something from CF's "temp" folder (which exists in a common path on most CF servers). Using this approach combined with some clever uploading a good hacker could even get arbitrary code to execute.

Finally, if using application.cfc just isn't your cup of tea you could pretty easily use a custom tag instead. A custom tag has a start and end "execution mode" - so inside your tag you can have:

<cfif thistag.executionmode IS "start">
... write out your header code
<Cfselseif thistag.executionmode is "end">
... write out your footer code
</cfif>

To use it you would bracket your path_info include like so:

<!--- name of your custom tag --->
<!--- spits out header here --->
<cf_mytagfilename>

<!--- tacks on content from this include into the buffer --->
<cfinclude template="#cgi.path_info#"/>

<!--- spits out footer here --->
</cf_mytagfilename>

This and many other approaches are used with ColdFusion. CF is extremely flexible and provides many possibilities when it comes to this sort of thing. That's both a blessing and a curse I suppose :) Good Luck!

Problem

I'm trying to figure out the best way to include both a header file and a footer file on a page without having to include two cfincludes on every page. Basically, I want the current page to just be the content. I found a way that accomplishes that by putting the following code in the application.cfm file, but I'm wondering if there are any performance or technical implications of doing it this way. Is there a better way of accomplishing this? application.cfm ``` <cfset application.header = "/someApplication/applicationHeader.cfm"> <cfset application.footer = "/someApplication/applicationFooter.cfm"> <cfinclude template="#application.header#"> <cfinclude template="#CGI.PATH_INFO#"> <cfinclude template="#application.footer#"> <!--- somewhat of a hack to make the page content load with the header and footer automatically inserted in the proper places. The cfabort prevents the actual page from being loaded a 2nd time. ---> <cfabort> ```

Original source