CITguy

DIY Coder

© 2014. All rights reserved.

« Back

Gotchas: cfsavecontent and cfoutput

Resources


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 <cfoutput>
2   <cfsavecontent variable="foo">
3     Gozirra!
4   </cfsavecontent>
5   #MyFunction(foo)#
6 </cfoutput>

The reason for this is because the cfsavecontent isn’t set until the cfoutput block closes. Here’s the ugly fix.

1 <cfoutput>
2   <cfsavecontent variable="foo">
3     Gozirra!
4   </cfsavecontent>
5 </cfoutput>
6 <cfoutput>
7   #MyFunction(foo)#
8 </cfoutput>

Happy Coding!