cf_buffer

My knowledge of ColdFusion 8 (or hell, even 7 for that matter) isn’t terribly broad. So if what I’m about to cover can be accomplished with an existing CF function or tag please let me know.

Output Buffering.

It’s something I first got use to with PHP. What it does is allow you to set a start and end point in your program and any output generated between those two points gets put into a variable which you can then manipulate further before outputting the content.

Recently I was asked if this was possible to do with ColdFusion. I couldn’t think of any existing feature that does this so I put together a (surprisingly) short custom tag that would do just that.

Here it is:

<cfif CompareNoCase( thistag.ExecutionMode, "start" ) EQ 0>
  <cfparam name="attributes.buffer" default="variables.buffer">
<cfelse>
  <cfset "caller.#attributes.buffer#" = thisTag.GeneratedContent>
  <cfset thisTag.GeneratedContent = "">
</cfif>

I put this into a file named buffer.cfm. Now anything that occurs between an opening and closing CF_BUFFER tag will be stored in a variable which can then be further manipulated however you want.

I did find something similar using getPageContext().getOut().getString(), but nothing simpler.

You might argue that this feature shouldn’t be needed in the first place. That whatever the need is it can be solved by redesigning the application. That might be true. But it’s a handy feature to play around with.