ColdFusion Quickie - Leaving an include

This post is more than 2 years old.

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:

<h1>Pre cfinclude</h1>

<cfinclude template="cfa_test.cfm">

<p> I'm past my include. </p>

My initial template has 2 blocks of text that surround a call to a cfinclude. Here is the cfinclude:

<!--- Should I leave early? ---> <cfif randRange(1,2) is 1> <cfexit method="exittemplate"> </cfif>

<p> Hello from the include. </p>

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Phillip Senn posted on 8/23/2010 at 8:17 PM

Does method="exittemplate" have any significance?

Comment 2 by Raymond Camden posted on 8/23/2010 at 8:18 PM

Yep, it means to leave the current template. You use exitTag when leaving a custom tag.

Comment 3 by Daniel Budde posted on 8/23/2010 at 8:35 PM

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.

Comment 4 by Mike posted on 8/23/2010 at 8:38 PM

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

Comment 5 by Stephen Weyrick posted on 8/23/2010 at 10:59 PM

That's pretty cool. I didn't know that you could do that, thanks for sharing.

Comment 6 by JC posted on 8/24/2010 at 12:39 AM

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?

Comment 7 by Raymond Camden posted on 8/24/2010 at 12:41 AM

I believe cfexit hasn't changed since 4.5.

Comment 8 by sanoop posted on 8/24/2010 at 2:36 PM

Ray, that is really a cool stuff. Thanks for sharing.

Comment 9 by John Allen posted on 8/25/2010 at 10:01 PM

That is pretty cool. Had no idea. Thanks.