ColdFusion Method Attribute Output=True Does Not Jive With EnableCFOutputOnly
I was talking to Michael Dinowitz last night at the NY-CFUG about the ColdFusion CFSetting attribute, EnableCFOutputOnly. We had just seen an example where a ColdFusion user defined function had its "Output" attribute set to "True" and also had CFOutput tags inside the method body as well. Michael argued that this was redundant as the Output attribute did just that - created a CFOutput-like environment. I agreed that this was true, except for when EnableCFOutputOnly was turned on; in that case, the additional CFOutput was required. I think he disagreed, but I don't quite remember. Regardless, here is the test:
<!--- Set it up so that only output inside of CFOutput will be rendered. ---> <cfsetting enablecfoutputonly="true" /> <!--- Define a ColdFusion method. The output attribute is set to true. This should create a CFOutput-like environment for the method body. ---> <cffunction name="OutputValue" access="public" returntype="void" output="true" hint="Outputs the given value."> <!--- Define arguments. ---> <cfargument name="Value" type="string" required="true" /> <!--- Output value. ---> <cfoutput>#ARGUMENTS.Value#</cfoutput> <!--- Return out. ---> <cfreturn /> </cffunction> <!--- Test what gets output. ---> <cfoutput> This value should be output: </cfoutput> <!--- Call a method that has OUTPUT=TRUE set. ---> <cfset OutputValue( "Stars When you shine, you know how I feel." ) />
As you can see, part of the content is wrapped in a CFOutput tags and part of it relies on the OUTPUT=TRUE attribute of the ColdFusion user defined method. Running the above code gives us:
This value should be output:
Now, if we go in and update the UDF to have a CFOutput tag:
<!--- Output value. ---> <cfoutput>#ARGUMENTS.Value#</cfoutput>
... and then run the code again, we get:
This value should be output: Stars When you shine, you know how I feel.
So, you can see here that OUTPUT=TRUE in a method has no effect if CFSetting has EnableCFOutputOnly enabled.
But, that's settings and methods that are in the same variables scope, if you will (not sure how else to say that). One of the other things that I discussed very briefly with Michael was the affect of CFSetting on a separate object such as a CFC or a custom tag. Again, I am not sure we agreed on this.
To test, I created a CFC for testing output both to the SAME output buffer and also to a completely separate file buffer:
<cfcomponent output="true"> <cffunction name="TestOutput" access="public" returntype="void" output="true" hint="Outputs the given value."> <!--- Define arguments. ---> <cfargument name="Value" type="string" required="true" /> <!--- Output value. ---> #ARGUMENTS.Value# <!--- Return out. ---> <cfreturn /> </cffunction> <cffunction name="TestFileOutput" access="public" returntype="void" output="true" hint="Outputs the given value to a file."> <!--- Define arguments. ---> <cfargument name="Value" type="string" required="true" /> <cfset var strOutput = "" /> <cfsavecontent variable="strOutput"> <cfoutput>Value:</cfoutput> #ARGUMENTS.Value# </cfsavecontent> <!--- Output value to file. ---> <cffile action="APPEND" file="#ExpandPath( './cfoutput_test.txt' )#" output="#strOutput#" /> <!--- Return out. ---> <cfreturn /> </cffunction> </cfcomponent>
Now, let's tweak the original code to use the Test ColdFusion component:
<!--- Create an instance of the Test CFC. ---> <cfset objTest = CreateObject( "component", "Test" ) /> <!--- Output the intro. ---> <cfoutput> This value should be output: </cfoutput> <!--- Try outputting value from the CFC. ---> <cfset objTest.TestOutput( "Stars When you shine, you know how I feel." ) /> <!--- Try outputting value to file (from the CFC). ---> <cfset objTest.TestFileOutput( "Stars When you shine, you know how I feel." ) />
Running this, again we get:
This value should be output:
But, now, if we check the "cfoutput_test.txt", we get this:
As you can see, with the CFSetting EnableCFOutputOnly attribute enabled, it affects not only things in the same scope and output buffer, but ALL calls made during that request (after the CFSetting tag of course) including buffers built for CFFile usage. Very interesting. This goes to show you that you should be very careful when using EnableCFOutputOnly as its effects are quite far reaching.
Want to use code from this post? Check out the license.
What I had said was that UDFs and CFCs run in their own 'space' and what happens within them should not be effected by a CFSETTING. On the other hand, the content outputted to the page would be affected. I've run some tests that got very different results than what you have.
Oh, and the term argued seems to be a loaded one. We whispered it back and forth during a presentation. :)
Shhhhhh! You don't want people knowing we weren't paying attention :)
Yes, I did not mean "arguing". Just that we were discussing (over the span of about 7 seconds). Personally, I hope that your opinion is more correct as it seems silly to me that a CFSetting in one file should affect the CFFile action of component... something ain't right about that (though I guess you could argue both ways).
weren't paying attention? I think the two of us were responsible for at least half of the 'write it down to ask later' questions. Some of our questions will either go to BD docs or make them rewrite parts of their server core. :)
I'm still seeing strange results from my tests that still say that UDFs are not effected by CFSETTING. On the other hand, assigning a UDF result to a variable on a page with a CFSETTING seems to be effected.
Note: Both Michael and I seemed to be on the right track - depending on variable usage. Michael is logging this inconsistent behavior as a bug.
The word 'jive' in your heading is, I think, in error. The word you wanted was 'gibe'. Of course, its five years later now, but what is that when your words will be immortal?