Using Pound Signs Properly ColdFusion Help

When working with expressions it is important that you use pound signs (#) correctly. Pound signs haw a special meaning in ColdFusion and, although they are used frequently in templates developers often use them incorrectly or unnecessarily.

Pound signs can be used to separate expressions from plain text. When expressions surrounded by pound signs are evaluated, the result is substituted for the expression.

The most common example of this is using pound signs to output variables or other expressions using CF OUTPUT .F or instance the following code generates an output of 1:

<CFSET Value ~ 1>
<CFOUTPUT>#Value#</CFOUTPUT>

You need to consider correct usage in several situations like these

• Within CF OUTPUT tags
• Inside tag attributes
• Inside strings
• Inside other pound signs
• Inside expressions

Within CFOUTPUT Tags

The most common place to see pound signs is within CFOUTPUT tags so that is the best place to start a discussion of using pound signs. Within CFOUTPUT tags you can use pound signs to output the value of a single variable or a single function call. For instance consider the following code:

<CFSET Value – 1>
<CFOUTPUT>
IValud<BR>
‘Exp(l)#
</CFOUTPUT>

This code produces the following output:

1
2.71828182846
If you remove the pound signs, then the text of the expressions themselves is output:
Value
Exp(1)

A limitation exists in the use of pound signs within CFOUTPUTtags: No complex expressions built out of combinations of values and operators can be used. Only single variables or function calls are valid inside pound signs within a CFOUTPUTblock.

Therefore using #Value# is valid within a CFOUTPUT block but using #Value + 1# is not.

Nothing prevents pound sign expressions from falling immediately adjacent to one another:

<CFOUTPUT>IValue”Exp(l)I</CFOUTPUT>

Inside Strings

Inside strings you can use pound signs to display the value of a single variable or a single function call. For instance the following code

<CFSET Value – 1>
<CFSET Result – ‘The result is ‘Value”>

causes the final value of Resul t to be The resul t ;s L

If you prefer you can concatenate the value of the variable and avoid the use of pound signs:

<CFSET Result – ‘The result is ‘ & Value>

The key difference here is that the variable is used outside the string and therefore the pound signs are not needed.

As with CFOUTPUT complex expressions containing operators cannot be used and adjacent pound sign expressions are allowed.

One point to note is that because expressions within pound signs are evaluated before . the rest of the string any quotation marks within the pound sign expression do not need to be escaped even though they appear within a string:

<CFSET Result – ‘The result is ‘LeftC’Test’,l)”>

Inside Tag. Attributes

Inside tag attributes, the same rules apply to the use of pound signs as within strings:

<CFPARAM NAME-‘Value’ DEFAULT-‘The result ;s ‘Exp(l)”>

The only point to note is that if you are assigning the value of a single variable or function call to an attribute it is more efficient not to enclose the pound sign expression within quotation marks:

<CFPARAM NAME-‘Value’ DEFAULT-tExp(l)’>

Inside Expressions

It is wise not to use pound signs when it isn’t necessary to do so.~or instance within expressions there usually isn’t a need to use pound signs to evaluate a variable value or a function call

In the following. example:
<CFSET.Value – 1>

<CFSET Result – Value + Exp(l».

it is possible but unnecessary and inefficient to use pound’signs:

<CFSET RESULT – ‘Value’ + .Exp(l)’)

Inside Other Pound Signs

At times it may be necessary to use pound signs inside of other pound signs. This situation generally arises when an expression is ~what complex

Consider the following example: .

<CFSET Valuel – ‘This is’)
<CFSET Value2 – ‘a test’>
(CFOUTPUT>
The string is “Valll.it ‘Value2″<8R>
In reverse: ‘Reverse(“Valuel’ ‘Value2,’)’
</CFOUTPUT>

This produces the following output:

The string is ‘This is a test’

In reverse: tset a si sihT

Here you need 1:0 include variables inside pound signs inside a function that is already enclosed in pound signs. This is’ a legal use of nested pound signs.

Generally though it is better to take alternate approaches for the sake of clarity. There are two options. First you can use concatenation of the variables to avoid using nested pound signs:

<CFSET Valuel – ‘This is’)
<CFSET Value2 – ‘a test’)
<CFOUTPUT>
The stri ng is’ I’ia1uel# #Va 1ue2#’ <BR>
In reverse: #Revt!rse(Valuel & ‘ , & Value2)#
</CFOUTPUT>
Even better, though, you can assign the string to another variable:
<CFSET Valuel 5 ‘lnis is’)
<CFSET Value2 – ‘a test’)
<CFSET TheString 5 ‘#Valuel# #Value2#’)
<CFOUTPUT>
The string is ‘#TheString#'<BR>
In reverse: #Reverse(TheString)#.
</CFOUTPUT>

A common misconception is that attributes to functions need pound signs..This is almost never the case, and will generally produce an error; For instance, #Reverse(TheStr; n9)# is valid. #Reverse( #TheSt I’in9#)# is not. Only if you wanted to put the argument within quotation marks to make it part of a string would you use the pound signs, as in #Reverse( “Test #TheSt r; n9#”)#. However as we already discussed, there are alternatives to this that are preferable.

Considering Special Features of ColdFusion Expressions

Some additional features of ColdFusion expressions warrant consideration before we close our discussion. These are:

• Order of precedence
• Typeless expression evaluation

Order of Precedence

The order in which operators appear in complex expressions is important. Expressions are not simply evaluated from left to right but rather follow rules of precedence. Under the rules of precedence operators are evaluated in the following order:

1. Unary +. Unary –
2. ”
3. *. /
4. \
5, MOO
6. +, –
7. &
8. EQ. NEQ. LT. LTE. GT. GTE, CONTAINS. DOES NOT CONTAIN
9. NOT
10. AND
11. OR
12. XOR
13. EQV
14. IMP

It is important to keep this order in mind when writing your expression. For instance  consider the following expression:

1 + 2·* 3

If you write this expression expecting left-to-right evaluation, then you would expect that 1 + 2 would be evaluated to 3. and then 3 * 3 would be evaluated, resulting in 9. However, the rules of precedence tell us that first, 2 * 3 is evaluated to 6. and then 1 + 6 is evaluated, with a final result of 7

If you want to override the rule of precedence in a particular instance, you can use parentheses. The sub-expression with parentheses will be evaluated before the rest of the expression (although within the parentheses the rules of precedence still apply). For instance you can make the preceding example evaluate from left to right as desired using parentheses:

(1 + 2) * 3

.This expression now evaluates to 9.

Posted on November 14, 2015 in Writing Expression

Share the Story

Back to Top
Share This