CFSaveContent And THISTAG.GeneratedContent Tip

Posted February 18, 2008 at 7:27 AM by Ben Nadel

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:

  • <!---
  • 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:

  • <!---
  • 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.



Reader Comments

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

Another good tip Ben, thanks


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

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


Mar 17, 2011 at 2:12 PM // reply »
2 Comments

Apparently, you cannot change THISTAG.GeneratedContent when THISTAG.ExecutionMode EQ "start"


Mar 17, 2011 at 2:26 PM // reply »
11,238 Comments

@Ralph,

Correct - there is no generated content at that point since the body of the tag has not yet executed.


Apr 24, 2012 at 12:33 PM // reply »
4 Comments

How come when I try to use ThisTag.generatedContent it does not seem to want to work?

I have a tag called <sample1>..

Here is a test using it:

  • <cf_sample1>This is a Test</cf_sample1>

Here is the contents of the tag file:

  • <cfif (thistag.executionMode eq "end")>
  • <cfoutput><br>value=#thistag.generatedContent#</cfoutput>
  • </cfif>

I get value=
but no value at all!


Apr 24, 2012 at 3:07 PM // reply »
4 Comments

@Orville,

Found my own problem!

In my Application.cfm file I am using:

  • <cfsetting enablecfoutputonly="Yes">

So, in order for my custom tag to work as I would expect, I have to call it like so:

  • <cf_sample1><cfoutput>This is a Test</cfoutput></cf_sample1>

Then it works just fine!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools