文章作者 : Kakusoo [ linkfoxo@yahoo.com ] Web URL : http://
上载日期 : 2000-11-21
The Situtation: You want to create your own functions to use across several CF pages
The Solution: We're all anxiously awaiting the debut of true UDFs. 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?">
|