Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to put user defined functions

Status
Not open for further replies.

forumposters

Programmer
Aug 31, 2006
61
US
Where do you keep your user defined functions? Do put them all in one file under a 'lib' folder and then cfinclude that file? Or, do you store them in a component and invoke that component? What is the best practice?
 
1. you can create UDFs within your pages as in

<cfscript>
function greet() {
var greeting = "Hello!";
return greeting;
}
</cfscript>

and later make a call it.

2. create a function within a component:

<cffunction name="greet">
<cfset greeting = "Hello">
<cfreturn greeting>
</cffunction>

then you can use cfinvoke to call that function.

take a look at this article:



hope it helps



 
Thanks for that link. I like the idea of simply putting them in a functions.cfm file and then cfinclude that file only on the pages where those functions are used.
 
what I tend to do is to put UDF's into a seperate directory (udfs) under the main root and then read the file into the app scope and store the function(s) there. we found that this speeded things up for us and prevented the need for the file to be read in every time we needed it.

you code would look something like this:

<cfif NOT isDefined('Application.myUDF')>
<cfinclude template = "udfs/myudf.cfm">
<cflock type="Exclusive" scope="Application">
<cfset Application.myUDF = variables.myudf>
</cflock>
</cfif>

<cflock type="Readonly" scope="Application">
<cfset Request.myUDF = Application.myUDF>
</cflock>

Hope this helps!

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top