Ask a Jedi: Custom Tags, OnRequestStart, UDFs, and Antimatter Engines

Asa asks:

I have a question for your ColdFusion Holiness. I have a file containing all my UDFs and I'm including it in onRequestStart: <cffunction name="onRequestStart">
<!--- Include Global Functions --->
<cfinclude template="/global_functions.cfm">
</cffunction>
But the functions are not available inside my Custom Tags. A Google search said something about the wrong variable scope. Doesn't onRequestStart get called for custom tags too? What's the best way to be able to use them in my custom tags?

There are a few things in play here, so let’s tackle it one by one. First off, if you cfinclude a file of UDFs via onRequestStart, the UDFs will not be available to your CFM files. It won’t be… unless you also have onRequest. The presence of onRequest in Application.cfc has the side effect of copying your Application.cfc methods and variables into your templates Variables scope. So I’d be willing to bet you did this - and noticed you could run your UDFs just fine until you ran a custom tag. Custom tags have their own Variables scope. You could access the Variables scope of the parent by using the Caller scope. So to run a UDF named turnBritneyOn, you would use:

<cfset result = caller.turnBritneyOn()>

This is icky though. What I recommend instead is a simpler approach. Copy your UDFs into the Request scope. This lets custom tags run the UDFs easier as well. So take this simple, very short UDF library:

<cfscript> function doItBritneyStyle() { return "Did it again..."; } </cfscript>

I’d modify it like so…

<cfscript> req = structGet("request.udfs"); function doItBritneyStyle() { return "Did it again..."; } req.doItBritneyStyle = doItBritneyStyle; </cfscript>

The req=structGet line simply creates a pointer to a structure called request.udfs. If the structure doesn’t exist, it is created. If it does exist, I simply get a pointer to it. This lets me then do req.UDF = UDF after each udf as a quick way to copy each UDF into the request scope.

On my templates and custom tags, I can then just do:

<cfset result = request.udfs.doItBritneyStyle()>

Lastly - onRequestStart, or any of the other Application.cfc methods, are not called for a custom tag call since a custom tag simply runs more CF code. It doesn’t start a new request. The same applies to calling a UDF or cfincluding a file.

Oh… antimatter engines. Yes, I did mention them. This post actually has little to do with antimatter, but after reading this list today, it’s definitely on my mind: Stupid Plot Tricks

My favorite: 140. If I board a derelict ship, and it appears that the former crew and passengers all died in some horrible fashion, I will immediately leave the ship, destroy it, and toss the wreckage into the nearest stellar object.

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