Skip to main content
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Sharon DiOrio
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Sharon DiOrio ( @sharondio )

Awesome ColdFusion Custom Tag Tip : CFExit Method / Loop

By on
Tags:

I just randomly happened upon this great ColdFusion custom tag tip over on Doug Hughes' site. I never realized this, but there is a LOOP value for the CFExit tag's method attribute:

<cfexit method="LOOP" />

Who knew? Apparently not me. Thought I would pass it on here in case anyone else missed it. Anyway now that Doug has armed me, it's time to start the experimenting :) Sweeeeeet.

Want to use code from this post? Check out the license.

Reader Comments

198 Comments

It's important to know the difference between <cfexit method="loop" /> and <cfbreak />.

<cfbreak /> is designed to exit out of a loop in the current template.

<cfexit method="loop" /> is designed for custom tags running in the "end" execution mode and it continues processing from first child in calling template.

15,674 Comments

Dan,

I have to admit, when I first read about the <cfexit method="LOOP" /> I was pretty pumped up... but when I started to think about how I could experiment with it, I came up short. I have seen examples, but didn't really understand them. To me, if I am going to iterate over something, I might as well just pass it into a function or a custom tag and do the iteration internally. I am not sure what advantage physically looping the tag would do.

I thought maybe it would be cool to be able to loop over something and have a custom "renderer" for each indexed item... but then I thought, why would I go through the overhead of a custom tag... why not just loop over it manually via a CFLoop of some sort and then use a custom tag renderer.

Anyway, it's still interesting and I want to learn how to leverage it well.

15 Comments

Seems like it should be method="endTag", or something. I just ran into a situation where I'd like to be able to exit the current iteration without breaking the loop. Here's the idea:

<cfloop from="1" to="20" index="i">
Process some stuff
<cftry>
Try something here that may cause an error
<cfcatch>
Ooh, error. It's not critical, but we can't continue. So we'll simply log it and alert someone.
<cflog>
Exit this iteration and continue processing
<cfexit method="loop">
</cfcatch>
</cftry>
If the above try/catch was successful, we'll do some more stuff.
</cfloop>

I could wrap cfif tags around everything, but that's ugly ;)

15,674 Comments

@Brad,

I see. You want something like a CFContinue tag. I have seen many people request something like that.

But, be careful; CFExit has to do with template-level execution, not loop execution. I think you are a bit confused as to what it is supposed to be doing with a custom tag.

15 Comments

Yeah. I'm clear on what it does. I use CFExist quite often to exit tags and templates. I just thought method="loop" is a little misleading. It's funny, I've been using CF for 7 years, and I've never used a custom tag to loop over something.

15,674 Comments

@Brad,

Yeah, I didn't use it for the longest time. The only reason I have used it to -date is to create custom CFLoop-style tags.

2 Comments

I tested the cfexit on a loop and would like to confirm that it exits the template, not the loop. Perhpas the author meant to say that it exits the loop as well as the page.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel