Normally I’m with the crowd who says “don’t output content or use <cfinclude> from within a function”. Like a lot of people I would much rather return whatever value I need to the calling page/function, so that I then have a choice with what I want to do with it.
Still, the other day I found myself doing what I don’t normally do…wanting to output a simple text value from within a method.
My scenario was quite complex so I won’t go into all the details, suffice to say that there were several methods being called eventuating in a final method call to display().
The display() method was outputting a simple string value (yikes!). Now the actual problem I encountered was that my output wasn’t actually displaying
I had output=”true” in the display() method but nothing was showing. It took me a few minutes to trace through the code and discover that because display() was being called from another function (and I wanted to output a value from within display(), that the initial functions output attribute also needed to be set to true. This I did not know.
So the following will not work because output is set to false in function1():
<cffunction name="function1" access="public" returntype="void" output="false">
<cfscript>
//no output in this method, just call another method
display();
return;
</cfscript>
</cffunction>
<!--- PRIVATE METHODS --->
<cffunction name="display" access="private" returntype="void" output="true">
<cfsavecontent variable="content">
<h1>Header</h1>
Content to display
</cfsavecontent>
<cfoutput>#content#</cfoutput>
<cfreturn />
</cffunction>
But if you change the output attribute to true:
<cffunction name="function1" access="public" returntype="void" output="true">
Lovely output!
Now if only I kept true to my rule of not outputting content in a method…

3 Comments
I’ve been struggling with this myself and I don’t like output in cffunction. Unfortunately for me, Peter Bell has a very good argument over in his blogs for using CFC for presentation….
http://www.pbell.com/index.cfm/2006/12/6/Architecting-Your-Views-Part-3-A-Proposed-Solution
(PS, I came from fullasagoog and your CSS ain’t working for me, it is when I visit your site directly tho)
Hi dc,
>your CSS ain’t working for me
Yeah I’m in the middle of finding a new host, as usual it’s a long process!
Sorry about that
While I understand your concerns about outputting display within a method, I don’t see what is wrong with putting a reusable piece of display code this way in a CFC if that’s what you see fit.
I for one use this in several situations, but instead of dumping the string out with output=”true” I return the string so I can decide what to do with it afterwards as you mentioned you like to do otherwise. Then you have a more conventional usage.
An example of where I use this is form validation. I have a core FormValidation CFC that manages validation rules and performs validation on a structure.I then have a FormValidationUI object that allows me to make a conveniently pluggable (and consistent) display for my forms when I add validation to them. The FormValidationUI object has getDisplayHtml() and getJavascript() functions to for my resulting display code.
Anyway, great post!
Mike.