Declaring UDFs in custom tags
The title of this post may suggest to the reader that I am writing about something everyone at least moderately familiar with Cold Fusion should know. But, for the unaware, Cold Fusion compiles cfscript first, then it compiles the rest of the cfml. With this in mind, know that if you have a custom tag which has a closing tag, you are in fact calling your tag twice. This is where an ordinarily functional tag will have problems with udf’s declared in cfscript. Cold Fusion is not evaluating whether the tag is running in thistag.executionmode start or end at the time the cfscript is compiled. When this happens, the compiler will gripe about not being able to declare udf’s more than once.
I usually address this issue one of two ways:
- If the UDF could be used more than once I’d add it to a global repository of UDFs and load it into the server scope. More on that in a later post.
- If the UDF is specific enough to the custom tag being declared to not warrant adding it to the global repository I create a routines.inc file, declare the function there, and use cfinclude to include it from the custom tag. The compiler doesn’t gripe about declaring the tag more than once, because it is no longer in a cfscript block in the custom tag.
Final Note
If the custom tag does not have a close tag, you can still declare the function in a cfscript block within the tag. I, however, still prefer to use a separate file. I usually place my custom tags in their own folders to keep everything neet, but I’ll have more on how I organize my tags and functions in a later post. Until then, dream in digital.
No related posts.




I use BlueDragon.NET, and it doesn’t exhibit this particular problem. But I would think you might be able to solve it like this as well:
function foo() {return “foo”;}
or maybe
function foo() {return “foo”;}
I know the later used to work in CF5 for working around this issue, but I thought the issue was resolved in MX… Did they do something in CF8 that resurfaced this or something? Or am I wrong and it was never addressed in MX? Its been too long since I played with CF, cant remember.
@Heath Provost
eck. Tags got stripped. Let me try it again
<cfif NOT isCustomFunction(”foo”)>
<cfscript>
function foo() {return “foo”;}
</cfscript>
</cfif>
or maybe
<cfif thistag.executionmode EQ “start”>
<cfscript>
function foo() {return “foo”;}
</cfscript>
</cfif>
@Heath Provost
Interesting, you of course seem to be correct. So, either I have completely lost my mind lately; which is of course entirely possible. Or the griping about declaring functions twice happens only under certain circumstances.
My curiosity was peeked by your comment, so I just tried to create a custom tag with only this as its body:
<cfif thistag.hasEndTag and thistag.executionMode... eq "start" or not thistag.hasEndTag> <cfscript> function foo(){ return "foo"; } </cfscript> </cfif>I also tried with isCustomFunction(”foo”) in the conditional, that worked fine as well. So, looks like I’ve been overlooking some other issue all this time. Because the above worked fine, though many times I have written custom tags only to have CF bitch me out about having declared my functions more than once.
I’ll investigate my particular circumstances more, and if I discover what about my tags force me to declare my function block outside I’ll followup post.
Thanks for your insightful input, as always.
Im not sure what BD does with this kind of situation, but somehow they handle it internally without requiring any kind of conditional declaration. Knowing how these things get called, it almost seems like you would HAVE to have the declaration be conditional - not sure how they deal with it. I think Im going to dig in the source code and see if I can fathom what they are doing.
@Heath Provost
That is actually how I do it myself… … works alright for what I need.