Matt Liotta and Barney Boisvert raised an interesting issue today on cfcdev. We all know, or should know, that cfc methods (and tag based udfs) take an OUTPUT attribute. This supposedly does two things: If set to true, it allows any and all output to be generated by the UDF. If set to false, it acts as if a cfsilent tag surrounded the function. In general, you should almost always use output="false".
However, something odd happens if you leave off the output attribute. It is supposed to default to true, but that is not the case. Consider the following code:
<cfset x = 1>
<cffunction name="test">
hi ray #x#
</cffunction>
<cffunction name="test2" output="true">
hi ray #x#
</cffunction>
<cfset test()>
<cfset test2()>
When test() is run, the string inside the function is output, but #x# is NOT evaluated. It acts just like normal text in a CFML page. When test2() is run, the value of x is evaluated. It acts as if the text were surrounded by cfoutput tags. This could lead to confusion as you may actually want to show #foo# or output an internal anchor link.
Archived Comments
Now I know you aren't reading my articles! ;-) I wrote about this in my CFC Tips article (number 7) on oreilly.com back in September:
http://www.oreillynet.com/p...
Actually, it isn't supposed to default to anything. See the documentation, they specifically made the output attribute have three states.
It's what I get for skimming, not reading, but at least I know I wasn't alone in missing it. ;)
The docs say:
output - Optional - Function is processed as standard CFML
This attribute is used only for a component.
º yes: the function is processed as if it were within a cfoutput tag
º no: the function is processed as if it were within a cfsilent tag
The docs correctly state the behavior of both output="yes" and output="no" but they do not say what the default is or what the behavior is when you omit output= - unhelpful tho' that is, the ISO standards word for this is "undefined behavior" which means all bets are off... :)
I'll take the issue up with the CF product team (and the docs team).
Sean, you must be reading the wrong documentation.
From livedocs for CFMX 6.1...
Specifies under which conditions the function can generate HTML output.
yes: The entire function body is processed as if it were within a cfoutput tag. Variables names surrounded by number signs (#) are automatically replaced with their values.
no: The function is processed as if it were within a cfsilent tag
If you do not specify this attribute, the function body is processed as standard CFML. Any variables must be in cfoutput tags.
Matt, try not to be such a smartass all the time... :)
Here's the docs I was quoting:
http://livedocs.macromedia....
Sounds like you found a page that explicitly specifies three behaviors? Let us know what it is and I'll pass that on to the product and docs teams along with my previous message. Thanx!
Looks like this is the page you're quoting?
http://livedocs.macromedia....
Correct, but I thought it was clear that I was quoting from the CFMX 6.1 documentation on livedocs.
Now we have the URLs for both references, it is clear - it wasn't before. We both should have quoted our references first time around (I did at least quote verbatim!).
I simply surfed over to livedocs for CFMX 6.1 and copied and pasted what the documentation said for the output attribute. I further indicated that it was from livedocs for CFMX 6.1. Granted I didn't include the URL, but there is only one URL that has the documentation for cffunction for CFMX 6.1 on livedocs, so I thought it was pretty obvious.
Thanks guys. Very informative AND entertaining to boot! ;)
Matt, you say "there is only one URL that has the documentation for cffunction" but I've already quoted *two* URLs with documentation for the output= attribute :)
Come on Sean! I stated, "Granted I didn't include the URL, but there is only one URL that has the documentation for cffunction for CFMX 6.1 on livedocs, so I thought it was pretty obvious."
The two URLs you quoted were for CFMX and CFMX 6.1 respectively. Please read my comments more carefully as I specify CFMX 6.1 on purpose.
Yup, hadn't noticed they were from different versions since I'd located them just by searching for "livedocs cffunction". Interesting that the 6.0 docs didn't mention the behavior of omitting the attribute but the 6.1 docs do mention it.
As you pointed out on the CFCDEV list, a lot of people don't know about this subtle behavior and even reading the docs it's easy to miss since that's effectively just a one-line change between versions...
I wonder if the behavior did actually change between 6.0 and 6.1 or whether it was just a doc change?
same behavior in 6.0, I fixed this with
<cfcomponent>
<cfsetting enablecfoutputonly="yes">
<!--- functions here --->
<cfsetting enablecfoutputonly="no">
</cfcomponent>