Skip to main content
Ben Nadel at the New York ColdFusion User Group (Jan. 2010) with: Clark Valberg and Javier Julio
Ben Nadel at the New York ColdFusion User Group (Jan. 2010) with: Clark Valberg ( @clarkvalberg ) Javier Julio

CFSaveContent And THISTAG.GeneratedContent Tip

By on
Tags:

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.

Want to use code from this post? Check out the license.

Reader Comments

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!

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!

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel