Bug in ColdFusion's cfcontent within CustomTag execution end

Posted by Dan on Jun 11, 2010 @ 3:02 PM

I was working on a ColdFusion custom tag this afternoon, that is to throw an error if tag is not properly constructed. In order to clean out the output, I'm using the following ColdFusion code snippet to clear the output buffer:

<cfcontent reset="true" />

This usually works great, but for some reason the output was always been displayed twice. The code was not actually executing twice, just being displayed in the output stream twice when the custom tag was in the "end" execution method.

To illustrate this problem, save the following as "contentBug.cfm":

<cfif (thisTag.executionMode is "start")>
  <cfparam name="attributes.reset" type="boolean" default="false" />

  <!---// if we have an end tag, stop processing now (the rest of the code will be run on the tag end) //--->
  <cfif thisTag.hasEndTag>
    <cfexit method="exitTemplate" />

<cfif attributes.reset><cfcontent reset="true" /></cfif>
<cfdump var="#thisTag#">
<cfabort />

Now save the following in a file called "test.cfm":

<cf_contentBug reset="true" />
Pay attention to the tag syntax, the "/>" makes the code execute in the "end" execution method where the bug reveals itself.

When you run the test.cfm template, you'll see the following output:


Note that the "test!" data outputted twice. If you change the "reset" attribute to "false" or change the code to <cf_contentBug reset="true"> (no ending slash) the code is only generated once.

This bug shows up in both ColdFusion v8.0.1 and v9.0.

If I find a workaround (other than not using <cfcontent reset="true" /> in my code) I'll make sure to update this post!

Categories: HTML/ColdFusion


  • Ahhhh, I see the problem I think - it's the wrong CFExit method. You want:

    <cfexit method="exittag" />

    "ExitTemplate" simply stops the current mode of execution, not the entire tag.

    That said, something still feels "buggy" though because if you exit the template (hault the START mode of execution), you still shouldn't see the output twice - it would only jump to the body (which you don't have) and then on to the END mode execution, which would only show once. Hmmm, I think.
  • Oh sorry, I didn't see the comment you had - right, you are using the correct CFExit method... ok, now I'm back to being stumped.
  • Ok, so if I remove the CFAbort at the end, this starts to work correctly. After the CFAbort, ColdFusion must be confused as to what to do with the remaining buffer content? Perhaps it just dumps it out?
  • So, I tried to create a simple-as-possible case where this happens. I created a custom tag that only has the following:

    <cfif (thisTag.executionMode is "end")>
    . . . . <cfcontent reset="true" />
    . . . . Hello in Tag!
    . . . . <cfabort />

    This will duplicate the output. Remove the CFContent OR the CFAbort and it works fine. Have them both, and you definitely get some sort of bug.
  • when you call the custom tag remove the trailing /

    <cf_contentBug reset="true" />

    Should work great
  • Roe,

    That's the point of the bug. For my actual code, the end execution is required. This is just a simplified example to show off the issue. The point is it should work, but doesn't.
  • I'm having the same problem currently. I had a query that I moved into a custom tag. Now I get duplicate results for some odd reason. I wanted to see if the code was being ran twice, it's not. Just outputted twice for no apparent reason. I am not using the cfcontent or cfabort tags within my custom tag either.
  • BTW I was using a cfmodule call to invoke my custom tag and my problem ended up having the same solution as listed above in the listed "work around." I removed the trailing slash in the cfmodule tag and the code now only appears once. Glad I found this post. Saved me some time and headache. Thanks!
  • Removing the trailing slash worked for me too (CF8). Thank you!!
  • Good to see I'm not nuts, I'm dealing with the same problem. So at least its a bug :-)

    In my case I need a closing tag for my custom tag so I cannot remove the slash or end tag (having nested custom tags inbetween). I'm developing a configurable report table tag that should also output the table for download as an excel-file, so discarding any output before the main tag, and completely aborting at the end of the tag, would be neat to avoid any disturbing HTML from the calling page. So with that bug it looks like the calling page unfortunately must also know when its Excel-download time and avoid any post-html... I let you guys know if I should find a workaround...
  • If anyone comes across this post and is still looking for a solution, I was able to get this to work by passing a variable to cfcontent.

    This worked:

    <cfcontent reset="yes" variable="#charsetDecode(MYCONTENTSTR,'utf-8')#"><cfabort>

Add Comment

Leave this field empty