Dynamic Expressions and Strings ColdFusion Help

To build dynamic expressions, you first need to consider some dynamics of strings. Dynamic expressions are built out of string expressions. String expressions are nothing more than regular ColdFusion expressions inside a string (in other words, inside quotation marks).
For instance, consider the simple expression 1+2. This can be converted into a string expression by surrounding j by quotes: “1+2”. This is a subtle difference but an important one. Consider the following Cold Fusion code

<CFSET Value1 – 1 + 2)
<CFSET Value2 – ‘1 + 2’)
Value1: tValue1#
Value2: tValue2#

This produces the following results:

Value1: 3
ValueZ: 1 +

Why should the two results be different? In the first CFSET tag, the expression 1 + 2 is evaluated (resulting in 3) and is assigned to Val ue1. But, in the second example the expression “1 + 2′ evaluates to a string (the string 1 + 2) !Vhich is assigned to Value2. The arithmetic expression 1 + 2 within the string is not evaluated here; the string expression is the only expression that is evaluated.”

By way of example, recall the simple CFSWITCH code used earlier in this chapter:

<CFSET NewNum – Num1 + 1>
<CFSET NewNum – NumZ + 1>
<CFSET NewNum – Num3 + 1>

We couldobtain the value of NewNum in a simpler matter if we could create a string containing •Num#Fo rm , Numbe r# + l’ which will be •Num1 + i-. ·Num2 + l’ or •Num3 + 1 n depending on the value of Form, Number and then simply evaluate the string as an arithmetic expression. This is exactly what dynamic expressions can do:

<CFSET Expression  ‘Num#Form.Number# + 1’>
<CFSET NewNum – Evaluate(Expression»

Dynamic expressions are all about evaluating this inner expression within the string expression. However, some complexities are introduced when you attempt to take a standard ColdFusion expression and embed itinto a string (in other words, put it inside quotation marks). These complexities arise out of the need to escape certain characters. We discussed escaping in general in Chapter 6 but here you need to review escaping and how it works.

Consider the following standard ColdFusion expression:

‘Hello’ & ‘there’

If you want to put this expression inside quotation marks to convert it to a string expression as you did with 1 + 2 earlier, you might want to try using “

“Hello’ & ‘there”

But, as you learned in Chapter 6, to include a double quote mark inside a string that is enclosed in double quotes, you need to escape the quote marks inside the string. This means repeating each double quote inside the string:

Inside the string, you have four pairs of escaped double-quote marks (underlined so .that they are easy to identify). Each of these represents one of the double quotes in the anginal expression.

You can improve readability slightly by alternating single and double quotation marks. There is no reason a string can’t be enclosed in single quotation marks. If a string is enclosed in single quote marks, then double quote marks can be used inside the string without being escaped; in this case, single quotes would need escaping. Therefore, you can eliminate the pairs of double quotes by escaping your expression in single quotes:

“Hello • ” ‘there”

In the first example, the original expression has double quotes to surround the expression and includes a third double quote inside the string. In the converted string expression, all’double quotes are escaped and an additional pair of double quotes must surround the whole expression. The original expression text has three double quotes and the string expression equivalent has eight double quotes
The same is true for the second example. All the single quotes must be escaped and an additional pair ‘of single quotes must surround the entire expression. You start with three single quotes and end up with eight single guotes.

The third example is quite different; it has a mixture of double and single quotes. As a rule, you escape the quotes within the original, which are the same type of quotes used to enclose it when converting it to a dynamic expression. Therefore, for this example, if
the surrounding quote is a single quote, you escape all single quotes.

The last example is similar to the third one. The only difference is that the surrounding quotes are double quotes. So, all double quotes in the expression must be escaped.

Posted on November 16, 2015 in Evaluating Dynamic Expressions

Share the Story

Back to Top
Share This