tags:

views:

475

answers:

3

I thought CFC's methods defined as functions in cfscript are output=false by default, but when I open the CFC in cfcexplorer.cfc (browser directly to the CFC), it says Output: enabled.

cfcexplorer.cfc's Bug?

A: 

I'm not entirely certain, but my guess would be that script functions are the same as cffunction tags in this regard - in that the default is neither true nor false.

Setting the output attribute for a cffunction, the following are the case:

  • true is equivalent to the function being wrapped in cfoutput.
  • false is equivalent to the function being wrapped in cfsilent.
  • Leaving it default (undefined) is equivalent to standard code that is wrapped neither with cfoutput nor cfsilent.

However, I almost never use cfscript, and this may not actually be the case - we'll have to wait for others to come along and either confirm or correct this.

Peter Boughton
+3  A: 

cfscript functions are a weird monkey. They are kind of both. You can't specify that they are output="false", but they are until you use a writeOutput(), but they are reported by cfcexplorer as being output="true". It is an odd issue I think the cfml advisory committee is looking at right now.

Jayson
I think this is the correct answer, but can someone confirm? thank you.
Henry
Actually, I would say both Peter Boughton's answer and my answer are more correct.
Nathan Strutz
+11  A: 

Short answer: It doesn't matter.

cfscript does not output anything unless you explicitly call writeOutput() from it. This includes functions in cfscript as well as any cfscript code outside of a function.

This is different from CF tags' syntax, which, by default, output at least whitespace between the tags. In cfscript, any text you write will be compiled by the CFML engine. in CF tags, any text you write will be written to the output buffer and sent to browser.

Long answer: It's the same as not specifying an output attribute.

cfscript blocks don't output anything. Any tag blocks, unless wrapped in cfsilent, do output whitespace if nothing else. Yes, even cffunctions do, but the output is discarded if the output attribute is set to false.

The essence of Peter Boughton's answer is correct. It's neither wrapped in cfsilent nor cfoutput. Output is not forbidden, but it doesn't happen unless you do it explicitly.

You can always combine a tag-based cffunction with scripting to get the best of both worlds. Something like...

<cffunction name="x" output="false" access="package">
    <cfargument name="y" type="string" required="false" default="YY" />
    <cfscript>
        var someValue = y & "something";
        writeOutput("test"); // this will not be written
        return someValue;
    </cfscript>
</cffunction>

This lets you specify an output and access on the cffunction tag as well as allow arguments to be optional (which you can't do through cfscript functions), then fill the body with cfscript, including var statements and the function return.

Of course, for that function, if you remove the output attribute or change it to true, it will output "test" before returning.

Nathan Strutz