Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store common functions in ColdFusion

We are running ColdFusion MX7.

One problem we have is that we have a lot of functions that we use in a lot of our pages. It would be nice to have them live in the 'Global' ColdFusion scope rather than having to include them in all of our pages.

Is there a way to do this that does not involve custom tags or the like?

I know we could attach some objects to the Application or Server scopes, but then we have to reference them as such.

Simply adding them to the global scope would be perfect.

EDIT

Thanks to the suggestions, here is what I came Up with. Basically, for each request in the OnRequestStart function, assign to a properly named variable in the client scope the function reference (this.functionName).

Application.cfc:

<cfcomponent OUTPUT="FALSE">
<cfset This.name = "MyApp">
<CFSET This.clientManagement = true>
<CFSET This.SessionManagement = true>

<CFFUNCTION NAME="Coalesce" OUTPUT="FALSE" access="public">
    <CFARGUMENT NAME="ARG1">
    <CFARGUMENT NAME="ARG2">

    <CFIF ARG1 NEQ "">
        <CFRETURN ARG1>
    <CFELSE>
        <CFRETURN ARG2>
    </CFIF>
</CFFUNCTION>

<cffunction name="onRequestStart">
    <CFSET CLIENT.COALESCE = this.COALESCE>
</cffunction>

</cfcomponent>

The pages that are under this application happily respond to the call:

<CFOUTPUT>#COALESCE("ONE","TWO")#</CFOUTPUT>

Works great!

like image 947
Tom Hubbard Avatar asked Nov 18 '25 00:11

Tom Hubbard


1 Answers

There's no such thing as "global scope".

If you're talking about variables scope in every page, you can try including the UDF's inside Application.cfm.

If you use Application.cfc, look up onRequest() in the CF7 doc.

like image 199
Henry Avatar answered Nov 19 '25 15:11

Henry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!