Pound signs (#) have special meaning in ColdFusion. When a CFML application page is processed, ColdFusion treats text delimited by pound signs differently from plain text.
Two simple and very important points about pound signs in CFML are:
For example, to output the current value of a variable named "Form.MyFormVariable," you must delimit the variable name with pound signs:
<CFOUTPUT>Value is #Form.MyFormVariable#</CFOUTPUT>
When ColdFusion processes an expression, it replaces the text of the expression and the two pound signs around it with its resulting value. In the example above, the expression #Form.MyFormVariable# is replaced with whatever value has been assigned to it.
While the guidelines for using pound signs in CFML are simple, there is still some possibility for confusion to arise. This is particularly true in cases where expressions and plain text are mixed together. The following sections provide more details on how pound signs should be used in CFML.
Expressions containing a single variable or a single function can be used freely inside CFOUTPUT tags as long as they are enclosed in pound signs.
<CFOUTPUT> Value is #Form.MyTextField# </CFOUTPUT> <CFOUTPUT> The name is #FirstName# #LastName#. </CFOUTPUT> <CFOUTPUT> Cos(0) is #Cos(0)# </CFOUTPUT>
If pounds are not used around these expressions, the expression text rather than the expression value will appear in the output generated by the CFOUTPUT statement.
Note that two expressions inside pound signs can be adjacent to one another, as in
<CFOUTPUT> "Mo" and "nk" is #Left("Moon", 2)# #Mid("Monkey", 3, 2)# </CFOUTPUT>
Complex expressions involving one or more operators cannot be inserted inside CFOUTPUT tags. The following example will produce an error.
<CFOUTPUT>1 + 1 is #1 + 1#</CFOUTPUT>
To insert the value of a complex expression in the output generated by a CFOUTPUT statement, use CFSET to set a variable to the value of the expression and use that variable inside the CFOUTPUT statement, as is shown below:
<CFSET Result=1 + 1> <CFOUTPUT>1 + 1 is #Result#</CFOUTPUT>
Expressions containing a single variable or a single function can be used inside strings as long as they are enclosed in pound signs.
<CFSET TheString="Value is #Form.MyTextField#"> <CFSET TheString="The name is #FirstName# #LastName#."> <CFSET TheString="Cos(0) is #Cos(0)#">
ColdFusion automatically replaces the expression text with the value of the variable or the value returned by the function. For example, the following pairs of CFSET statements produce the same result:
<CFSET TheString="Hello, #FirstName#!"> <CFSET TheString="Hello, " & FirstName & "!">
If pound signs are not used around these expressions, the expression text as opposed to the expression value will appear in the string. For example, the following pairs of CFSET statements produce the same result:
<CFSET TheString="Hello, FirstName!"> <CFSET TheString="Hello, " & "First" & "Name!">
As in the case of the CFOUTPUT statement, in strings two expressions can be adjacent to each other, as in
<CFSET TheString="Monk is #Left("Moon", 2)##Mid("Monkey", 3, 2)#">
Note that the double quotes around "Moon" and "Monkey" need not be escaped (or doubled, as in ""Moon"" and ""Monkey""). This is because the text between the pound signs is treated as an expression that is evaluated first before its value is inserted inside the string.
Complex expressions involving one or more operators cannot be inserted inside strings. The following example produces an error:
<CFSET TheString="1 + 1 is #1 + 1#">
To insert the value of a complex expression inside a string, use CFSET to set some variable to the value of the expression and use that variable inside the string, or use the string concatenation operator:
<CFSET Result=1 + 1> <CFSET TheString="1 + 1 is #Result#"> <CFSET TheString="1 + 1 is " & (1 + 1)>
To insert the pound character inside a string, use two pound signs as shown below:
<CFSET TheString="This is a pound sign ##.">
The rules for using pound signs inside strings apply to the use of pound signs inside tag attribute values. The following example demonstrates the point:
<CFCOOKIE NAME="TestCookie" VALUE="The value is #CookieValue#">
If the value of a tag attribute is a variable, function, or array element, use the following syntax:
<CFCOOKIE NAME="TestCookie" VALUE=#CookieValue#> <CFCOOKIE NAME="TestCookie" VALUE=#CookieValueArray[Index]#>
This usage is more efficient than VALUE="#CookieValue#".
There are very few cases in which pound signs can be nested inside the same expression. Usually, the need for nested pound signs arises because of the high degree of complexity of the expression. The following example shows a valid use of nested pound signs:
<CFSET Sentence="The length of the full name is #Len("#FirstName# #LastName#")#">
The pound signs need to be nested so that the values of the variables FirstName and LastName are inserted in the string whose length the Len function will calculate. Generally, the existence of nested pounds implies the presence of a complicated expression. For example, the above piece of CFML could be rewritten to improve its readability:
<CFSET FullName="#FirstName# #LastName#"> <CFSET Sentence="The length of the full name is #Len(FullName)#">
A common mistake is to put pound signs around the arguments of functions, as in:
<CFSET ResultText="#Len(#TheText#)#"> <CFSET ResultText="#Min(#ThisVariable#, 5 + #ThatVariable#)#"> <CFSET ResultText="#Len(#Left("Some text", 4)#)#">
All of the above statements result in errors. As a general rule, never put pound signs around function arguments.
Allaire recommends that pound signs be used only where necessary. The following example demonstrates the preferred method for referencing variables.
<CFSET SomeVar=Var1 + Max(Var2, 10 * Var3) + Var4>
It is a cleaner and more efficient method.
In contrast, note the following example, which uses pound signs unnecessarily:
<CFSET #SomeVar#=#Var1# + #Max(Var2, 10 * Var3)# + #Var4#>