Ask a Jedi: Why would I use cfhtmlhead?

This post is more than 2 years old.

Jen asks:

i read your recent post on the cfhtmlhead tag and wondered if you might review why we would use the cfhtmlhead tag instead of a cfinclude? what are the pros and cons for each method?

As I said in that post, I'm not really a fan of cfhtmlhead so I may not be the best person to ask. I'll explain how I think it could be used.

Imagine a fairly typical ColdFusion page that looks like so:

<cf_layout title="Deep Thoughts by Paris Hilton"> code and logic for page go here </cf_layout>

This this page, the main logic/text/etc of the page is wrapped by a custom tag that handles the layout of the site. Imagine your code/logic/etc realizes that it's going to need Spry libraries. But at this point, the head of your template has already been output. In order to 'change the past' so to speak, you could simply use cfhtmlhead to embed the script tags to load Spry libraries.

Now normally I wouldn't do it like that at all. I'd have something like this instead:

needSpry = false if (logic here) needSpry=true

<cf_layout loadSpry="#needSpry#"> etc </cf_layout>

And in a Model-Glue style framework I'd do it like so:

if(logic here) viewState.setValue("needspry", true)

Which the Layout event would be able to pick up on.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA