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

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Feb 19, 2008 at 2:14 AM // reply »
16 Comments

Another good tip Ben, thanks


Feb 26, 2008 at 9:17 PM // reply »
1 Comments

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


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 12:10 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Ben Nadel, Are you saying that correct data doesn't have any business value? Nonsense. What about referential integrity and foreign keys? ... read »
Mar 19, 2010 at 12:00 PM
Using jQuery To Leverage The OnChange Method Of Inputs
Thnx. FYI spelling error in your comment "// Add dirtry flag to the input in" ... read »
Mar 19, 2010 at 10:57 AM
Javascript Number.toFixed() Method
It doesn't have a base() method, but it can be put together simply with: Math.base = function(n, to, from) { return parseInt(n, from || 10).toString(to); }; ... read »
Mar 19, 2010 at 10:21 AM
ColdFusion Query of Queries Unexpected Data Type Conversion
@Hiren, Nice use of cast. ... read »
Mar 19, 2010 at 10:17 AM
Why NULL Values Should Not Be Used in a Database Unless Required
@Mike, If you enjoy using NULL values, then go for it; from a technical standpoint, there's certainly no reason to not use them if you like them. All I'm saying is that I think a lot of times the ... read »
Mar 19, 2010 at 10:12 AM
ColdFusion Path Usage And Manipulation Overview
@Sean, Did you ever figure out the "/" issue? Sorry I had no more ideas on that problem. ... read »
Mar 19, 2010 at 10:07 AM
Using ColdFusion's CFLocation Tag For Inline Image SRC Attributes
@Daniel, So, when a browser is making a HTTP request, does it read the headers and then potentially shut down the HTTP request if the ETag is a certain value? Or, is it actually making two requests ... read »
Mar 19, 2010 at 10:05 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
@Dmitry, I'll have to take a look at that. Typically, I just use CFHTTP, but I check out more Java-based approaches. ... read »