<cfexit method="exittag" /> QuackFuzion
Posted on January 5, 2008 at 1:32 AM in ColdFusion
While I've been writing ColdFusion for over 10 years now, I am a relative newcomer to the world of writing my own custom tags. I know, odd; but it is what it is. On occasion, I run into a particular behavior with <cfexit /> that perplexes me greatly, and I'm hoping that someone can help shed some light on it for me.
Here's a brief snippet from the top of the custom tag...
- <cfif thisTag.executionMode IS NOT "start">
- <cfexit method="exittag" />
- </cfif>
As I understand the LiveDocs on this one, if the tag's execution mode is not "start", thereby being "end", the tag should stop executing and revert execution back to the caller, picking up immediately after the closing tag, and continuing from there.
So, if this code is in the caller...
- State<br />
- <select name="state" id="state">
- <option value=""></option>
- <cfmodule template="/cfMgmt/tags/forms/states.cfm" defaultState="TX" />
- </select>
- </div>
- </div>
- </form>
... then when the tag is hit again (executionMode = "end"), the tag should not process anything else, but the calling template would pick back up with
- </select>
- </div>
- </div>
- </form>
and build the rest of the page.
Notice the " />" at the end of the <cfmodule> tag? That's simply habit. XML and XHTML, which I write tons of, both require explicit ending tags. So, out of habit, I do the same thing with my ColdFusion tags as well. What I always forget is that when doing so with cfmodule, the tag is run in "executionMode=end" as well as "executionMode=start", since the tag was explicitly closed.
So what is the problem? Well, for some reason, when this tag is encountered in this manner, it completely stops -all- execution, rather than just the execution of the custom tag. Even the closing <select> tag is not rendered. Why? I totally do not get it.
If you can help "learn me sumtin", I'd be most appreciative. :-)
Update
I have attached a zip file that contains a couple of files to demonstrate this odd behavior.
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]



On 1/5/08 at 6:31 AM, Michael Sharman said:
cfexit has 3 possible "methods":
1. ExitTag
2. ExitTemplate
3. Loop
When you call cfexit method="exitTag" in either "start" or "end" mode, ColdFusion will exit the custom tag and continue processing AFTER the end tag.
ExitTemplate jumps to the 'inactive mode' (in between start and end tags) if in start mode. If in end mode then it exits the custom tag and continue processing after the end tag.
Loop doesn't work if in start mode, if in end mode it jumps back to inactive mode (good when you have nested child custom tags).
Hope that helps!
On 1/5/08 at 8:57 AM, todd sharp said:
<cfif thisTag.executionMode eq 'start'>
do stuff...
<!--- if the tag *requires* an end tag --->
<cfif not thisTag.hasEndTag>
<cfthrow message="You need to close this tag dude!" />
</cfif>
<cfelse>
do something (or nothing)...
</cfif>
On 1/5/08 at 11:16 AM, Tony Petruzzi said:
Instead of doing the thistag.executionmode check like so:
<cfif thisTag.executionMode IS NOT "start">
<cfexit method="exittag" />
</cfif>
Just make <cfexit method="exittag" /> the last line of your custom tag.
Benefits of doing the way I'm suggesting:
1) less code so it looks cleaner
2) easier to remember
3) you don't have to use a condition so it executes faster (like 1ms, but what the heck)
On 1/5/08 at 3:36 PM, Matt Quackenbush said:
@ Todd/Tony - Thanks for the suggestions. Both make a lot of sense, and I shall likely use both of them in appropriate circumstances.
On 1/5/08 at 9:45 PM, Michael Sharman said:
Or anything else which might do this in the custom tag?
On 1/6/08 at 12:16 AM, tony petruzzi said:
For instance: I normally have a custom tag I use for layout and then some helper custom tags for form stuff. I've found in the past that I had to make sure that I was closing them correctly or weird things would happen, like the stuff you're describing.
On 1/6/08 at 7:05 PM, Matt Quackenbush said:
@ Tony - I'm not using any nested tags.
I've attached a zip file to the post with a couple of demo files. cfexit.cfm is the main template, and states.cfm is the custom tag.
On 1/6/08 at 7:27 PM, Michael Sharman said:
I believe it's because of your whitespace management.
At the top of your states.cfm you have <cfsetting enablecfoutputonly="yes" />, but if you exit the custom tag you still have this setting enabled which will therefore supress any output you have.
You can either put cfoutput around everything in the 'caller' page or more likely re-arrange your custom tag's cfsetting.
Hope that helps
On 1/6/08 at 7:37 PM, Matt Quackenbush said:
Thanks to everyone who took a look at it.
On 2/11/08 at 1:04 AM, ike said:
With regard to cfexit specifically though, if I have a custom tag that's not supposed to have an end tag, I'll always just put the cfexit at the end of the tag without the conditional around it -- that way it just exits at the end of the "start" mode instead of checking to see if it's in end mode first. And if you put the exit after your cfsetting where you re-enable the enablecfoutputonly flag, then that'll avoid this particular wierdness.