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?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Henry
  • 32,689
  • 19
  • 120
  • 221

3 Answers3

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.

Nathan Strutz
  • 8,095
  • 1
  • 37
  • 48
  • 1
    In CF9, functions defined in cfscript can have optional arguments. See [Defining components and functions in CFScript](http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSE99A664D-44E3-44d1-92A0-5FDF8D82B55C.html). – Jared Beck Jun 05 '12 at 00:14
  • 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. – Owen Allen Sep 14 '12 at 23:34
  • Amendment - you can put output = "no" on a cfscript function to squelch this behavior. – Owen Allen Sep 14 '12 at 23:41
  • 1
    output = "true" output = "false" // output not specified All have different behaviors – James A Mohler Nov 26 '12 at 22:55
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.

Jayson
  • 2,021
  • 3
  • 25
  • 26
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.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176