Skip to main content
Participating Frequently
April 24, 2011
Answered

Global UDF

  • April 24, 2011
  • 2 replies
  • 833 views

I have seen a few postings, but nothing difinative.

If I have a bunch of UDFs that I want accessable to all my pages, where do I put them?

According to this

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a66e-7fd5.html

It wants me to use an Application.cfm file, but I use the Application.cfc. (Why is Coldfusion even telling me to use Application.cfm in version 9?)

    This topic has been closed for replies.
    Correct answer talofer99

    You can use the <cffunction name="onApplicationStart"> of the application.cfc

    to do the following :

    <cfinclude template="includes/_udf.cfm">
    <cfset application.udf = structNew()>
    <cfset application.udf.PasswordStrength = PasswordStrength>

    My _udf.cfm file looks like this :

    <cffunction name="PasswordStrength" access="remote" output="false" returntype="struct">
      <cfargument name="password" type="string">

    ......

    </cffunction>

    This when ever I want to use the function I can use: application.udf.PasswordStrength("string")

    Hope this helps.

    2 replies

    Inspiring
    April 24, 2011

    Organise your UDF into libraries of related functions, in CFCs (eg; StringLib.cfc, FileLib.cfc, etc).  Instantiate the CFCs into application-scoped variables in onApplicationStart().

    --

    Adam

    talofer99
    talofer99Correct answer
    Inspiring
    April 24, 2011

    You can use the <cffunction name="onApplicationStart"> of the application.cfc

    to do the following :

    <cfinclude template="includes/_udf.cfm">
    <cfset application.udf = structNew()>
    <cfset application.udf.PasswordStrength = PasswordStrength>

    My _udf.cfm file looks like this :

    <cffunction name="PasswordStrength" access="remote" output="false" returntype="struct">
      <cfargument name="password" type="string">

    ......

    </cffunction>

    This when ever I want to use the function I can use: application.udf.PasswordStrength("string")

    Hope this helps.

    Participating Frequently
    April 24, 2011

    Thanks.

    These suggestions make since and should be included in CF documentation.

    I have updated the code to included the application structure to be created and populated in the same include page, that way if I want to reload the application UDF, i just have to point my browser to the UDFs page.