Cfinclude, a primer
I wanted to offer a simple primer in the use of cfincludes. I often find that newer users can stumble on their site development when it comes to how cfincludes work in ColdFusion.
Cfincludes are probably one of the most used ColdFusion tags around.
The function of the cfinclude tag is to grab a file that is saved somewhere else in your server’s directory structure and ‘include’ its contents in the place of the cfinclude tag.
So, for instance, if you have this file:
And you have this file saved in ‘includes/head/meta.cfm’
Then when the page is translated for the browser by ColdFusion, it will replace the include request with the contents of the include file.
This is simple enough. Hopefully you can see the benefits of having some of your code in an include file. It makes it much easier to update a website if all of your redundant code is centralized.
You may have noticed that I put the include file in a specific directory structure. This is something that I have learned helps to keep the code organized. ColdFusion doesn’t require this level of organization, but it is helpful to yourself and other ColdFusion programmers to organize your code.
ColdFusion usually loads on your server with the ‘/’ mapping already there. This is so that you can use the ‘/’ in your cfinclude as a mapping to your webroot, and anything after that ‘/’ in the template parameter will be treated as the directory location.
I like to add a specific mapping to my ColdFusion administrator that for my ‘includes’ location. This makes it possible to move the location of the includes folder for future website changes.
A cool feature of this is also that the mapping location of the includes folder doesn’t actually have to been inside the webroot.
Let’s say for instance that you have two seperate websites that use some of the same code.
Your web root locations might be:
c:/inetpub/website1
c:/inetpub/websiteTwo
then you can have a mapping to:
<cfset this.mappings['/includes'] = ‘c:/sharedResources/includes’ />
Now you would be able to use the same includes in both websites. If code changes are needed, then you would only need to change 1 location.
If you enjoyed this post, make sure you subscribe to our RSS feed!