Awesome ColdFusion Custom Tag Tip : CFExit Method / Loop
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.
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.
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.
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
Try something here that may cause an error
Ooh, error. It's not critical, but we can't continue. So we'll simply log it and alert someone.
Exit this iteration and continue processing
If the above try/catch was successful, we'll do some more stuff.
I could wrap cfif tags around everything, but that's ugly ;)
Correction... I should have said, I could wrap CFTRY tags around everything inside the loop.
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.
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.
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.
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.
Just wanted to add that I meant without specifying the method in the tag as was introduced here.