Is output=false by default for cfscript function in CFC?
Asked Answered
R

3

9

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?

Rubidium answered 10/6, 2009 at 20:39 Comment(0)
R
19

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.

Rustie answered 10/6, 2009 at 21:46 Comment(4)
In CF9, functions defined in cfscript can have optional arguments. See Defining components and functions in CFScript.Lomax
Sadly, this is not actually correct. Go ahead and run a SerializeJSON() on an ORM result in a cfscript function and you'll see 3 lines of whitespace. In a cffunction with output='no' there is no whitespace.Shon
Amendment - you can put output = "no" on a cfscript function to squelch this behavior.Shon
output = "true" output = "false" // output not specified All have different behaviorsDoorjamb
I
4

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.

Ingamar answered 10/6, 2009 at 21:16 Comment(2)
I think this is the correct answer, but can someone confirm? thank you.Rubidium
Actually, I would say both Peter Boughton's answer and my answer are more correct.Rustie
D
1

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.

Dilative answered 10/6, 2009 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.