Now a days it seems like every one talks about CFCs, or UDFs, and almost no one talks about custom tags. While they are a bit slower than CFCs or UDFs, custom tags are still pretty darn handy. Here are a few tips for people writing custom tags.
- Watch your whitespace.
This is more a general ColdFusion tip, but since custom tags tend to be used inside other documents (otherwise they wouldn't be custom tags), you tend to notice their white space even more, especially if the tag is used inside a loop. I recommend just using cfsetting on top and at the bottom of your file, although be sure to also use cfsetting if you leave your custom tag early.
- If your custom tag is not meant to be used in "wrapped" mode, then always include this line as your last line of code:
If you don't, and someone calls your tag like so, <cf_foo/>, then your tag will execute twice.
While I may use "cf_" format in my examples, I almost never use cf_ when invoking a custom tag. I use cfmodule. This tends to be a bit more wordy, but it means i don't have to worry about "name" confusion. This is where ColdFusion has to look for your custom tag, and could potentially find the wrong one. Not only that, ColdFusion caches the location of the tag, so if you move it, you need to restart ColdFusion. All of this goes away if you use cfmodule.
If you return a value, do not hard code it like so:
<cfset caller.foo = 1>
Rather, make it an attribute so the caller can specify what result to return. For example:
<cfparam name="attributes.result" default="result" type="variableName">
<cfset caller[attributes.result] = now()>
This lets me call the tag like so:
<cf_foo> <cf_foo result="result2"> <cf_foo result="result3">
At the end of those lines, I'd have 3 variables created: result, result2, and result3.