Brandon asks:
Is there a way to see what UDF's are available to the requesting template?
While there is probably some internal method that can do this, there is a little known function you can use to find out if a variable is a UDF. Let's start by creating a bunch of variables and UDFs.
<cfscript>
a = 1;
b = 2;
c = 3;
d = 'you betcha!';
function e() { return 'beer!'; }
f = [1,2,3];
g = {name='Ray',consuming=e};
function h() { return "more beer!"; }
</cfscript>
<cffunction name="i">
<cfreturn "Stuff">
</cffunction>
Yes, those are horrible variable names, but you get the idea. In the above list, e, h, and i are udfs. Everything else is not. So how can we find them? First lets treat the variables scope like a structure:
<cfloop item="k" collection="#variables#">
Next use the isCustomFunction function:
<cfoutput>
The variable #k# is
<cfif not isCustomFunction(variables[k])>NOT</cfif> a UDF.<br />
</cfoutput>
That's it! If you run this you will see that it correctly recognizes which variables are UDFs.
In case you're curious, there isn't a direct way to tell if "X" is a built in function. You can use getFunctionList, which returns a struct, but you would then obviously need to do a structKeyList followed by a listFindNoCase.
I'll admit to have never using either of these functions in production. Has anyone used them?
Archived Comments
Couldn't <cfdump var="#variables#" Label="Variables"> run on the calling template determine that?
E
Or just structKeyExists(getFunctionList(),"x")
-- that'll be faster than listFindNoCase("x",structKeyList(getFunctionList()))
The old StripTags function has this in it because of changes to regex in CF6
http://cflib.org/udf/StripTags
@Edward - I think he may be wanting to determine it programmatically in a situation where he's not sure if function "x" will exist when he writes the code, but wants to use it if it's available at run-time.
@Ike ... gotcha ... I wasn't considering his possible case scenario.
@EB - Yes, Ike is right, it was programmatic access.
@Ike - Duh. Can I blame it on being too early in the morning and me having a few too many Newcastles? ;)
@Ray - If you want to give people the impression that you load up on ale first thing in the morning, sure. :P
That might come in real handy if someone wants to determine which rogue UDF's may be causing problems in an application ... very cool.
Thanks Ray!
I am going to run that right now. Yeah, the app that I am building allows for custom "plugins" to be installed, and sometimes those plugins need to know if other plugins have been loaded first. So this function will allow them to find out the information.
Works perfectly! Thanks so much!