Chapter 4: ColdFusion Expressions: Operators and Other Constructs
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 ##.">
Pound signs inside tag attribute values
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#".
Nested pound signs
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)#)#">
583
Need help?
Do you have a question about the COLDFUSION 4.5-CFML LANGUAGE and is the answer not in the manual?
Questions and answers