How can I create User-Defined Functions?
The Situation: You want to create your own functions to use across several CF pages
Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors
|
|
The Solution: We're all anxiously awaiting the debut of true UDFs in version 5. Until then, the best we can do is to create custom tags that can be used like functions. Never created a custom tag before? Don't worry -- it's the soul of simplicity. Let's create one now that produces management decisions. This is useful when you don't have a manager around to help you. We'll save it as "InstaMgr.cfm".
First, we'll create a new ColdFusion page that does what we want.
<!--- Create the universe of answers. --->
<cfset answer1 = "Hmmm...better let me think about it.">
<cfset answer2 = "Sorry, HQ put the kabosh on all new spending.">
<cfset answer3 = "Sounds like a personal problem.">
<!--- All parameters passed to the custom tag/function are referred to with an
"attributes." prefix once within the scope of the tag. --->
<!--- ...to a manager all questions sound the same... --->
<cfset attributes.theQuestion = "Blah blah yadda yadda ho hum">
<!--- Select one... --->
<cfset num = RandRange(1,3)>
<!--- Return the answer to the caller. BTW, "caller." is the way you
return info from a custom tag to the file that called it.--->
<cfoutput>
<cfset caller.InstaMgr = "#Evaluate('answer' & num)#">
</cfoutput>
That's all there is to creating a custom tag. You can call it either with a <cf_ prefix (like <cf_InstaMgr>) or with a <cfmodule> tag. For example
<cf_InstaMgr
theQuestion = "Can we get some more resources on the mission critical project that must be done by next week?">
|