The other day on Twitter, I saw someone tweet about how they just discovered ColdFusion's CFHTMLHead tag and was planning to use it in a project. This gave me flash backs to a project I took over in which the CFHTMLHead tag was being used all over the place. Having not known about this tag at the time, I thought I was going crazy!! Stuff was showing up in the HEAD of the HTML document that wasn't there in the corresponding ColdFusion template and I couldn't for the life of me figure out where the heck it was coming from.
For anyone who has never seen the CFHTMLHead tag - please stop reading this post immediately - it takes text content and injects it into the end of the HTML HEAD element, assuming it has not yet been flushed to the client. For ease of use and demonstration, I am going to wrap the CFHTMLHead tag into a custom tag so that I can use the custom tag's generated content to set the CFHTMLHead text attribute:
<!--- Check to make sure this is the end of the tag. This way, we will have generated content to work with. ---> <cfif (thistag.executionMode eq "end")> <!--- Use the generated content for the data piped into the CFHTMLHead tag. ---> <cfhtmlhead text="#thistag.generatedContent#" /> <!--- Clear the generated content. We used it for the CFHTMLHead tag and have no need to output it now. ---> <cfset thistag.generatedContent = "" /> </cfif>
As you can see, the CFHTMLHead tag has one attribute, Text, which is the text output that will be injected into the HTML.
Now, let's take a look at how this works:
This is one of the few ColdFusion tags that I actually recommend people don't use. It just seems very hacky to me - like using it is a symptom of some flaw in your page architecture. However, if you are going to use it, I beg of you this one thing: document your use of it! Either put some sort of ColdFusion comment in your HEAD tag about where content might come from; or, even put a comment in your CFHTMLHead content:
Regardless of how you go about it, make sure that someone can look at either the ColdFusion page template or your rendered page source and gain some insight as to where this content is coming from. Otherwise, you'll be kicking yourself down the line (or get kicked by the developer that took over your project).
Want to use code from this post? Check out the license.