Gotchas: cfsavecontent and cfoutput
29 Aug 2010Resources
I was playing around with cfsavecontent to generate dynamic HTML variable values for use as an argument to a function when I came across an interesting behavior regarding the tag. The issue I ran was that Coldfusion was giving me an error that the variable saved by cfsavecontent was not defined when I was trying to output the value. Take a look at the code below:
This code will produce a “Variable not Defined” error in ColdFusion at line #5.
1
2
3
4
5
6
<cfoutput>
<cfsavecontent variable="foo">
Gozirra!
</cfsavecontent>
#MyFunction(foo)#
</cfoutput>
The reason for this is because the cfsavecontent isn’t set until the cfoutput block closes. Here’s the ugly fix.
1
2
3
4
5
6
7
8
<cfoutput>
<cfsavecontent variable="foo">
Gozirra!
</cfsavecontent>
</cfoutput>
<cfoutput>
#MyFunction(foo)#
</cfoutput>
Happy Coding!