Calling wrapped ColdFusion custom tags within CFIF blocks

This post is more than 2 years old.

That isn't the best title in the world but hopefully an example will help. Yesterday Simon Free encountered an interesting problem with wrapped custom tags. For folks who don't know what I mean, a wrapped custom tag is simply a custom tag that wraps something. So for example, consider the following code.

<cf_bold>This is overkill.</cf_bold>

The bold custom tag is running both at the beginning and end of the string. ColdFusion tells you in what 'mode' it is being run. Here is what bold.cfm looks like:

<cfif thisTag.executionMode is "start"> <b> <cfelse> </b> </cfif>

The ThisTag scope is a special scope you can use within custom tags. In my tag above I use the built in exectionMode variable to tell how the tag is running. Now let's thrown a wrench in the works. Consider a more advanced "fancy" bold tag. It may look like this:

<cfif thisTag.executionMode is "start"> <span style="font-size:20px;color:red"> <cf_bold> <cfelse> </cf_bold> </span> </cfif>

As you can see, I've wrapped a call to my bold tag and added some special CSS as well. However, when I run this I get:

ColdFusion doesn't like the opening and closing of the tags in different parts of the CFIF. So how can you solve this? It's a bit of a hack, but you can simply create a way to override the tag's execution mode. Consider this new version of bold.cfm:

<cfparam name="attributes.executionMode" default="#thisTag.executionMode#">

<cfif attributes.executionMode is "start"> <b> <cfelse> </b> </cfif>

All I've done here is switch to using an attribute called executionMode. It defaults to the thisTag version which means I can still use my simple wraps. But what about fancybold.cfm? I just changed it to this:

<cfif thisTag.executionMode is "start"> <span style="font-size:20px;color:red"> <cf_bold executionMode="start"> <cfelse> <cf_bold executionMode="end"> </span> </cfif>

As you can see, I now specify how the bold tag is run. A bit icky, but then again, it's hidden away in the custom tag. To the outside world it's still pretty easy:

<cf_fancybold>This is overkill.</cf_fancybold>

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 Tony Nelson posted on 12/30/2010 at 8:43 PM

Another approach would be to update the generated content inside the fancybold custom tag, like so:

<cfif thisTag.executionMode is "start">
<span style="font-size:20px;color:red">
<cfelse>
<cf_bold><cfoutput>#thisTag.generatedContent#</cfoutput></cf_bold>
<cfset thisTag.generatedContent = "" />
</span>
</cfif>

This allows you to leave the bold custom tag unchanged.

Comment 2 by Raymond Camden posted on 12/30/2010 at 9:33 PM

That's a good option too Tony!