Look at the following code and guess what the output would be:
<cfset newstring = strObj.getString() />
<cfoutput>#newstring#</cfoutput>
Oh, here's the cfc...
<CFFUNCTION NAME="init" ACCESS="public" RETURNTYPE="makestring">
<CFRETURN this>
</CFFUNCTION>
<CFFUNCTION
NAME="getString"
ACCESS="public"
RETURNTYPE="string">
<CFSET var thisString = "" />
<CFSAVECONTENT
VARIABLE="thisString">
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
</ul>
</CFSAVECONTENT>
<CFRETURN thisString />
</CFFUNCTION>
</CFCOMPONENT>
Choices:
- A:[empty string]
- B:
- list item 1
- list item 2
- list item 3
- C: depends
The correct answer is C, and what it depends on is whether or not somewhere else within your application a CFSETTING tag is lurking that has ENABLECFOUTPUTONLY set to 'Yes'. If this attribute is in effect, then your result will be A; if it's not, your result will be B.
The reason I felt the need to post on this is the fact that I spent a few hours yesterday developing a mild headache trying to figure out why my perfectly simple cfc was producing an empty string at one point within my app, but the correct string elsewhere. Through experimentation and noting that when I removed the CFSAVECONTENT tags everything worked as designed, I eventually concluded that I MUST have discovered a bug in the framework I was using, and so notified the keepers of the framework of all the details of my investigation and conclusion, along with code samples. They immediately (and nicely) informed me that more likely it was the fact that I had ENABLECFOUTPUTONLY enabled, and that I hadn't included CFOUTPUT tags within my CFC method. With egg still dripping from my face, I added the CFOUTPUT tags to my method and wouldn't ya know it...it worked.
I still haven't taken the time to find out where within this app the CFSETTING tag is being called, but two lessons gleaned here for me:
- Next time I feel excited at the prospect of having discovered a "bug", I'll think again (nobody wants the reputation of being The Boy Who Cried 'Bug!');
- Start troubleshooting from 10,000 feet instead of with a magnifying glass.
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Real world analogy:
You live in a house that has strict rules about adult content (ie. no magazines, no songs with bad lyrics, no adult televsion). But then you get a cable box with 600 channels. The televsion box doesn't automatically block adult content because it is contained within the house? No, it has to have a setting INSIDE of updated (ie. parental control).
Now, maybe that is a stetch of the imagination, to get that over to the world of ColdFusion, but it makes sense in my mind.