Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the New York ColdFusion User Group (Dec. 2008) with:

Awesome ColdFusion Custom Tag Tip : CFExit Method / Loop

By Ben Nadel on
Tags: ColdFusion

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.




Reader 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.

Reply to this Comment

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.

Reply to this Comment

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 ;)

Reply to this Comment

@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.

Reply to this Comment

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.

Reply to this Comment

@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.

Reply to this Comment

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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.