Start/End Mode and Custom Tags

As you know (or may know) any custom tag can be called in both ‘start’ and ‘end’ mode. What does this mean? When CF encounters the following code:

<cf_foo>
lots of code
</cf_foo>

The custom tag foo will actually be run twice. Inside the tag, you can check to see which mode you are in by examining thisTag.executionMode. This technique allows for many cool custom tags, but that’s not why I’m posting.

If you use cfmodule to call foo, you can still use ‘wrappers’ as I have defined above, you simply end the call with </cfmodule> instead of </cf_sometag>. However, if you have ANY cfmodule call inside your tag pair, CF can get confused by the </cfmodule> call. To get around that, you simply use a closing / at the end of the call:

<cfmodule template=”…” />

However - this will execute your tag twice. So, how do you get around it? You could check executionMode like so:

<cfif thisTag.executionMode is “start”>
lots of code
</cfif>

However, why wrap an entire tag in one big cfif? Instead, simply do:

<cfif thisTag.executionMode is “end”><cfexit></cfif>

This makes your code easier to read and simpler to debug.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless 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

Comments