Did you know that you can "leave" a cfinclude and return to a calling template? This may be useful in cases where you realize you no longer need to run the rest of the cfinclude and simply with ColdFusion to carry on back in the calling document. Consider the following simple example:
<cfinclude template="cfa_test.cfm"> <p>
I'm past my include.
</p>
<h1>Pre cfinclude</h1>
My initial template has 2 blocks of text that surround a call to a cfinclude. Here is the cfinclude:
<p>
Hello from the include.
</p>
<!--- Should I leave early? --->
<cfif randRange(1,2) is 1>
<cfexit method="exittemplate">
</cfif>
Now obviously this is a very contrived example, but basically if we 'fail' a 50/50 test we use the cfexit tag to leave the template. If we pass, we keep going on. I've used cfexit many times before, but only inside a custom tag. It never really occurred to me that it could possibly be used within templates as well. The idea came up in a meeting last week and I tested to confirm it works as advertised.
To be clear, the example above is kinda dumb. If you really were going to leave immediately it would perhaps make sense to move the check to the calling template so you don't bother even including it. But hopefully you can get the idea. cfexit is one those tags that doesn't get a lot of usage in most applications, but it can definitely be useful.
Archived Comments
Does method="exittemplate" have any significance?
Yep, it means to leave the current template. You use exitTag when leaving a custom tag.
Good tid-bit Ray. As you said, most of us wouldn't think to use it since we just control the cfinclude call with some sort of if statement.
Sounds useful, but I think I tend "guess wrong" when it comes to determining if I need to save processing time by skipping some logic. Using this would probably bite me in the behind quite a bit :-)
That's pretty cool. I didn't know that you could do that, thanks for sharing.
I KNEW there was some difference between cfexit and cfabort besides cfabort letting you output an error message. I haven't used it since 4.5 though. Did it do that back then or was that added later?
I believe cfexit hasn't changed since 4.5.
Ray, that is really a cool stuff. Thanks for sharing.
That is pretty cool. Had no idea. Thanks.