How to Actually use a UDF?

    October 17, 2006

Over the past few days I’ve had multiple people ask me the same question – and that means one thing to me… blog post!

The question these people were asking was the same – I know how to write a UDF, but not sure how to actually use it on a page? By that they didn’t mean the “function” syntax (x = foo()), but how to include the UDF so that it could be used on a page.

The answer is simple once you realize that a UDF is nothing more than another kind of ColdFusion variable. Consider this code:


What do you have to do to make this not throw an error? There are multiple ways to handle this. First, define it on the page:

<cfset x = "DJ Jazzy Jeff and the Fresh Prince">

Another way:

<cfinclude template="thisiswherexismade.cfm">

There are other ways of course, but you get the idea. So to use a UDF you follow the same rules. Here are two more examples using the same format as above:

function cic() { return "monkey"; }

And then the cfinclude version:

<cfinclude template="filewithcfcUDFinit.cfm">

Just like other variables, UDFs can be placed in the shared scopes. You can’t do it directly though but rather must reassign:

function dharma() { return "swan"; }
request.dharma = dharma;




Add to | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

Raymond Camden,

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long
time ColdFusion user, Raymond has worked on numerous ColdFusion books
and is the creator of many of the most popular ColdFusion community web
sites. He is an Adobe Community Expert, user group manager, and the
proud father of three little bundles of joy.