Using Dynamic Functions


ColdFusion features three dynamic functions, which are designed to facilitate the run-time creation and evaluation of expressions (and thus are dynamic). Using a dynamic expression to access or assign variables, it is possible to replace multiple lines of <cfif> and <cfset> statements with a single line of code. The dynamic functions are IIf(), DE(), and Evaluate(). All of these functions will be covered in the next few pages.

Refer to Chapter 2, "Working with Variables and Expressions," and Chapter 3, "Conditional Processing," for the CFML tag equivalents of the <cfscript> discussed here.


NOTE

There is one additional dynamic function, named SetVariable(), which can be used to set variables where the variable name itself is variable. This function is not covered here, as the need for it has been eliminated by enhancements to the core language and tags.


CAUTION

Dynamic functions take longer to parse and optimize than other functions do. Be aware that your performance will suffer when you use these functions.


IIf()

The name of the IIf() function is derived from the programming concept immediate if. The function is used as a powerful replacement for a <cfif> <cfelse> statement. The syntax of the IIf() function is as follows:

 IIf(condition, string expression1, string expression2) 

All three of these arguments are composed of ColdFusion expressions. The condition is the expression that will be tested. If the condition is trUE, then string expression1 is returned; if it is FALSE, then string expression2 is returned. Because IIf() returns something based on a Boolean operator (TRUE or FALSE), it is to be used in place of a simple IF/ELSE statement. Yet, because an IIf() occupies one line of code, it is much more concise than an IF/ELSE statement. Take the following examples:

Example 1: IF/ELSE
 <cfif DayOfWeek(Now()) IS 1>  <!--- we are closed on Sunday --->  <cfset bClosed=1> <cfelse>  <!--- we are open all other days --->  <cfset bClosed=0> </cfif> 

Example 2: IIf()
 <cfset IIf(DayOfWeek(Now()) IS 1,"bClosed=1","bClosed=0")> <!--- same as example 1, but on one line! ---> 

TIP

Although Example 2 is a true expression, there are times when a developer might want to return something printable. To return the string without evaluating it as an expression, single quotes can be used as follows:

 <cfset IIf(DayOfWeek(Now()) IS 1,"bClosed=1","bClosed=0")> <cfoutput>  #IIf(bClosed,"'We are closed!'","'We are open!'")# </cfoutput> <!--- The expressions will be returned without being evaluated ---> 

Note that the DE() function could also be used instead of the single-quote method. The DE() function will be explained in the next subsection.


It is important to understand that IIf() returns the result of the expression it has evaluated. Example 2's first use of IIf() might not appear to return anything at all, but it does. Setting a variable in ColdFusion returns the value of the variable itself. Therefore, the code in example 2 will return the number 1 if it is Sunday, and the number 0 if it is not.

DE()

The DE() function (which stands for delay evaluation) is mostly used within other dynamic functions such as IIf(). As you saw in the last subsection, there are times when control is needed over expression evaluation. If a certain function takes an expression as an argument, it will return the result of the expression after Cold Fusion has processed it. If, however, the developer intends to return the expression itself, without evaluation, the DE() function will prevent ColdFusion from conducting this evaluation. This is useful for a handful of reasons, the most obvious being the printing of a certain string within an IIf():

 <cfset IIf(DayOfWeek(Now()) IS 1,"bClosed=1","bClosed=0")> <cfoutput>  #IIf(bClosed,DE("We are closed!"),DE("We are open!"))# </cfoutput> 

This example shows that DE() can be used just as the single quotes were used in the previous subsection: to escape evaluation and return the string. But the DE() function does more than return literals. Imagine if an expression was contained within a variable. A hypothetical example of this might be a text box in which a user could type an expression, such as 2+2. The developer could check to make sure that the expression did not contain a divide-by-zero command, and then return a result. To better understand this example, look at the following code:

 <cfset FORM.expression="2/0"> <!--- pretend that the user typed in the above and submitted the form---> <cfoutput> #IIf(FORM.expression CONTAINS "/0",      DE(FORM.expression),      FORM.expression)# </cfoutput> 

The above code will print "2/0" as output. But if FORM.expression does not contain a command to divide by zero (division by zero is illegal), the output would be the result of the expression. For instance, 2+2 would result in 4.

Evaluate()

The Evaluate() function is used to return the result of an expression. Here is a simple example of the Evaluate() function:

 <cfset FORM.expression="2+2"> <cfset result=Evaluate(FORM.expression)> <cfoutput>  #FORM.expression# equals #result# </cfoutput> <!--- this will print "2+2 equals 4" ---> 

Multiple expressions can be passed to the Evaluate() function as arguments. Remember that when ColdFusion processes multiple expressions, it always returns the rightmost one.

TIP

An interesting way of using the Evaluate() function is to piece together expressions through concatenation. For example, you could allow users to create ColdFusion variables on their own and decide if they were to be structures or arrays. This could be done by piecing together the correct function names and keys/indexes based on what the user selected in a form. Look at the following example:

 <cfparam name="FORM.createArray" default="1"> <cfparam name="FORM.createStructure" default="0"> <cfparam name="FORM.variableName" default="myVar"> <cfset function1="ArrayNew"> <cfset function2="StructNew"> <!--- did the user want to create an array? ---> <cfif FORM.CreateArray IS 1>  <cfset Evaluate("FORM.variableName = " & function1 & "(1)")> <cfelseif FORM.CreateStructure IS 1>  <cfset Evaluate("FORM.variableName = " & function2 & "()")> </cfif> 

This logic actually creates a new array or structure by piecing together the correct combination of characters. If the user wants to create an array, you need to do an ArrayNew(1), and if you need to create a structure, you need to issue a StructNew(). These are accomplished by using string expressions and concatenation within the evaluate argument.


Evaluate() is not needed for simple expressions. For example, to display the sum of two variables, the calculation could be performed directly inline like this:

 <cfoutput>#var1+var2#</cfoutput> 



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net