Ask a Jedi: Does ColdFusion have a htmlfoot tag?

Rob asks:

Is there a <cfhtmlfoot> tag? One that would write at the end of html file, before </body> tag...

In case folks don’t get why he is asking, ColdFusion comes with a cfhtmlhead tag that lets you dynamically add stuff to the HEAD portion of an HTML document. There is not a corresponding tag like what Rob wants, but there is no reason we can’t hack one up in a custom tag. My solution will make use of both the Request scope and the oft-maligned (by me) onRequest function. First, a sample page:

<cf_htmlfoot text="<p>© Raymond Camden #year(now())#"> <html>

<head> <title>Test</title> </head>

<body>

<p> Woohoo,web design kicks butt. </p>

</body> </html> </code>

This is a trivial page with simple text on it. Note the call to the custom tag, htmlfoot, on top. The custom tag just does this:

<!--- the text to add ---> <cfparam name="attributes.text" default="">

<!— where we store it —> <cfparam name=”request.footertext” default=”“>

<!— add it —> <cfset request.footertext &= attributes.text> </code>

As you can see, we simply take your text, and append it to the text we want to add to the foot. This actually makes my tag better as I don’t think you can have multiple cfhtmlhead tags. If I weren’t so lazy, I’d also make the custom tag support this syntax:

<cf_htmlfoot> Foo Foo </cf_htmlfoot>

Anyway, the last step is to enable onRequest to notice the Request scope variable we created:

<cffunction name="onRequest" returnType="void"> <cfargument name="thePage" type="string" required="true"> <cfset var content = "">

&lt;cfsavecontent variable="content"&gt;
&lt;cfinclude template="#arguments.thePage#"&gt;
&lt;/cfsavecontent&gt;

&lt;cfif structKeyExists(request, "footertext")&gt;
	&lt;cfset content = replacenocase(content, "&lt;/body&gt;", "#request.footertext#&lt;/body&gt;")&gt;
&lt;/cfif&gt;

&lt;cfoutput&gt;#content#&lt;/cfoutput&gt; &lt;/cffunction&gt; </code>

There isn’t much to talk about here. All I did was look for the Request variable, and if it existed, I insert it into the result HTML before outputting it to the browser. Again, I’m not a big fan of onRequest, but this is an interesting example of how one could use it.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. 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 https://www.raymondcamden.com

Comments