[thelist] Tip: ColdFusion - UDFs, CustomTags, CALLER scope and evaluate()

Sean Brown sbrown at xacting.com
Sun Sep 22 11:48:01 CDT 2002


<tip type="ColdFusion" author="Sean Brown">
	In CF 5.0 + you can create User Defined Functions (UDFs) using CFML Script.
UDFs need to be parsed before they are referenced. In practice this means
cfinclude-ing a common library of often used UDFs in Application.cfm or
something similar.

	When you are in a CustomTag a UDF defined in this manner (parsed before the
CustomTag is invoked) will be in the CALLER scope. This is true whether you
are using evaluate or not. Since the only tips were about why not to use
evaluate(), the example shows an appropriate use of the function -- run-time
execution decision making.

Exmaple:
=====================

--- file - foo.cfm: ---
<cfscript>
	function isFoo(str) {
		if(str eq 'foo') {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	function isBar(str) {
		if(str eq 'bar') {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	function isBuzz(str) {
		if(str eq 'buzz') {
			return TRUE;
		} else {
			return FALSE;
		}
	}
</cfscript>

<cf_testStr str="bar"
		functions="isFoo,isBar,isBuzz">

--- file - testStr.cfm (CustomTag) ---
<cfloop list="#Attributes.functions#" index="thisFunc">
	<cfscript>
       	if(evaluate("CALLER.#thisFunc#('#Attributes.str#')")) {
			writeOutput("#thisFunc#: true <br>");
		} else {
			writeOutput("#thisFunc#: false <br>");
		}
       </cfscript>
</cfloop>
<cfoutput>
	calling w/o evaluate():<br>
	#CALLER.isFoo('foo')#
</cfoutput>

--- output: ---
isFoo: false
isBar: true
isBuzz: false
calling w/o evaluate():
TRUE

</tip>




More information about the thelist mailing list