ColdFusion Method Attribute Output=True Does Not Jive With EnableCFOutputOnly

Posted February 20, 2007 at 12:47 PM by Ben Nadel

Tags: ColdFusion

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:

Value:

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.



Reader Comments

Feb 20, 2007 at 3:35 PM // reply »
26 Comments

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. :)


Feb 20, 2007 at 3:40 PM // reply »
10,640 Comments

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).


Feb 20, 2007 at 3:50 PM // reply »
26 Comments

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.

Really strange.


Feb 20, 2007 at 3:56 PM // reply »
10,640 Comments

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.


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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »