You now understand the dynamics of building a dynamic expressAdd Newion: taking a standard ColdFusion expression and converting it into a string expression by following the necessary rules of escaping quote marks within the expression. However, you still don’t know how to evaluate these dynamic expressions. The simplest why to evaluate a dynamic expression is by using the Evaluate function. The Evaluate function enables you to carry evaluation of a dynamic expression out to its completion.
Let’s start with our simple 1 + 2expression. When converted to a string, you have ‘1+ 2’. In the following code, you assign the string expression to a variable-and then evaluate it using the Eva1uate function:
<CFSET Expression – ‘1 + 2’
The result is that 3 is output: The arithmetic expression contained within the string expression has been evaluated by the Eval uate function. Of course, this doesn’t seem to offer much in the way of additional functionality. After all, you could simply have assigned 1 + 2 to a variable and output the results:
Here, as well, the output is 3. In this case, the string expression is 1 + Increment and, because Increment is st;t to 2, the result of evaluating the string f’xpre sion with Evaluate is 3.
The power here is that you can reuse this string expression with different values of Increment in the same template:
<CFSET Expression – ‘1 + Increment’>
<CFSET Increment = 2>
The output would be
1 + 2 – 3
1 + 4 – 5
Each time you use Evaluate, the arithmetic expression in the string Expression is dynamically evaluated based on the value of Increment. There is a caveat here, though. You need to .take care in using pound signs inside string expressions.
F.or instance, consider the following example:
<CFSET Expression – ‘I + #Increment#’>
<CFSET InCrement – 4>
Based on the previous discussion, you might first think that the resulting output would be 5. However, a subtle and important difference exists between the string expression in this example and the one in the previous examples. In this case, when you assign a string value to Expression, you use pound signs around Increment inside the string expression. By doing this, you cause the value of Increment to be included in the string rather than the variable name. Therefore the string “1 + 2” (rather than “1 + Increment”) is stored in Expression.
Therefore, regardless of the value of Increment later in the template, using Eva1uate(Expressi on) will always result in 3. This highlights the subtle difference between the dynamic expression ‘1 + Increment’ and the expression “1 + #Increment#”, which is evaluated one time, after which the value can never change.
Other Dynamic Expression Functions
You can use three other functions in working with dy-namic expressions:
We will consider these briefly.
Using the DE Function
The Defunction is known as the ‘:!\~evaluation function. According to the Colusion documentation, the DEfunctioa ~ a string expression with quotes wrapped . around it and the necessary quotes within the string escaped. This can be used, in conjunction with Evaluate or !If (covered later in this chapter), to prevent evaluation of the resulting string expression. Distinction between using Evaluate with and without the Defunction.
The advantage of this function may seem limited in this context. However, it does provide one advantage: It does the work of enclosing an expression in quotation marks and performing the necessary escaping. Therefore, the assignment
<CFSET Expression – “Some “double” quotes’ ‘>
is ctionally the same as
<CFSET Value – ‘Some ‘double’ quotes’>
<CFSET Expression – DE(Value»
This proves useful because it enables you to avoid the often confusing task of enclosing your expressions. in quotes and handling all the escaping that it entails .
Using the Set Variable Function
The SetVariabl e function enables you to assign values to variables whose names are dynamically determined. For instance, if tile name of the variable that you wanted to assign a value to is stored in another variable, you could use SetVariable to achieve the task:
<CFSET VariableToSet – ‘SomeVariable’>
<CFSET Value – 2>
<CFSET Result – SetVariable(VariableToSet,Value) >.
Here, the value 2 is assigned to the variable SomeVariable. The value of Variable To Set and Value remain unchanged. In addition, the value that was just set (in this case 2) is returned by the SetVariable function, and you assign it to the variable Result. By way of further example, consider the following loop where the,dynamically generated variable is set to the Counter value:
<CFLOOP INDEX-‘Counter’ FROM-l T0-3>
<CFSET Result – SetVariabl (‘num*Countert’,Counter»
This produces the same results as the following CFSETtags used earlier in this chapter:
Using the Life Function
Based on the value of y, either x/y will be evaluated or •Error”, will be returned (when y is 0). Notice the use of the 1;>Efunction for Express; on1. If you don’t use Demand ‘Y is 0, then an attempt will be made to execute Evaluate(Expresson1), which is the ‘ same as Eva1uate(‘ Error’). This will cause ColdFusion to attempt to evaluate Error as a variable ..But, you haven’t created a variable called Error. You want the string’ Error’ as the result. So, you use Evaluate(DE(‘Error’)) to return the string Error”. Hence, the use of DEC Express; on1) in the If function.
Where Do We Go from Here?’
In the next chapter, you will look at ColdFusion scripting. ColdFusion scripting was a . new addition to ColdFusion with the release of version 4: It provides an alternative syntax for the basic programming constructs such as loops and conditional statements, ‘Although Coldfusion’s tag-based programming constructs are powerful, they can seem counter-intuitive and a little cumbersome for developers with programmihg experience. ColdFusion scripting offers a way to write Server-based scripts using a programming style and syntax that will seem more familiar to many experienced programmers.