Chapter 4: ColdFusion Expressions: Operators and Other Constructs
583
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)#)#">
Summary of Contents for COLDFUSION 4.5-CFML LANGUAGE
Page 1: ...Allaire Corporation CFML Language Reference ColdFusion 4 5...
Page 207: ...Chapter 1 ColdFusion Tags 183 CFCATCH CFTRY BODY HTML...
Page 224: ...200 CFMLLanguageReference CFOUTPUT P Text within CFOUTPUT is always shown CFOUTPUT BODY HTML...
Page 336: ...312 CFMLLanguageReference CFIF BODY HTML...
Page 404: ...380 CFMLLanguageReference DE It is morning CFOUTPUT P BODY HTML...
Page 413: ...Chapter 2 ColdFusion Functions 389 Customer BalanceDue BR CFOUTPUT CFIF BODY HTML...
Page 483: ...Chapter 2 ColdFusion Functions 459 CFOUTPUT CFLOOP BODY HTML...
Page 584: ...560 CFMLLanguageReference...
Page 594: ...570 CFMLLanguageReference...