CFSaveContent And THISTAG.GeneratedContent Tip

Posted February 18, 2008 at 7:27 AM

Tags: ColdFusion

Over the weekend, when I was working on my Dig Deep Fitness prototype, I thought of a neat little trick. I am sure that this has been done by many people, but it had never occurred to me. I was working with a ColdFusion custom tag in which I needed to replace the generated content of the tag. Normally, when I would do something of this nature, I would simply store the new content in a CFSaveContent tag buffer and then set that variable into the THISTAG.GeneratedContent variable:

 Launch code in new window » Download code as text file »

  • <!---
  • We only want to execute this after the tag has
  • executed. That way, we will know all the child
  • tags and content that has been generated.
  • --->
  • <cfif (THISTAG.ExecutionMode EQ "End")>
  •  
  • <!--- Create the new tag content. --->
  • <cfsavecontent variable="VARIABLES.NewContent">
  • <cfoutput>
  •  
  • <!---
  • Put the new content in here that you want the
  • ColdFusion custom custom to actually produce.
  • --->
  •  
  • </cfoutput>
  • </cfsavecontent>
  •  
  •  
  • <!--- Set the tag's new content. --->
  • <cfset THISTAG.GeneratedContent = VARIABLES.NewContent />
  •  
  • </cfif>

But this weekend, as I was writing a tag like that, it hit me! Why not just store the CFSaveContent buffer directly into the GeneratedContent of the tag? Why bother even going through an intermediary variable:

 Launch code in new window » Download code as text file »

  • <!---
  • We only want to execute this after the tag has
  • executed. That way, we will know all the child
  • tags and content that has been generated.
  • --->
  • <cfif (THISTAG.ExecutionMode EQ "End")>
  •  
  • <!--- Reset the tag content. --->
  • <cfsavecontent variable="THISTAG.GeneratedContent">
  • <cfoutput>
  •  
  • <!---
  • Put any content in here that you want the
  • ColdFusion custom custom to actually
  • produce. By storing the content diretly into
  • the GeneratedContent, it will set the output
  • that is displayed.
  • --->
  •  
  • </cfoutput>
  • </cfsavecontent>
  •  
  • </cfif>

Notice that the CFSaveContent tag is now storing its buffer directly into the THISTAG.GeneratedContent variable, which will, thereby, replace the content of the ColdFusion tag.

So, like I said, this is a really minor tip, but I think it has a big logical and visual impact on the layout of the ColdFusion custom tag. Thought I would share this with anyone who hadn't realized this shortcut yet.

Download Code Snippet ZIP File

Comments (2)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

Another good tip Ben, thanks

Posted by Michael Sharman on Feb 19, 2008 at 2:14 AM


It will come in handy. Go Ben go. :)

Posted by phpnuke on Feb 26, 2008 at 9:17 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting