Pound SignsPound signs (#) have a special meaning in CFML. ColdFusion treats text delimited by pound signs differently from plain text. When ColdFusion processes an expression, it replaces the text of the expression and the two pound signs around it with its resulting value.
For example, to output the current value of a variable named <cfoutput>Value is #Form.MyFormVariable#</cfoutput>
In the example above, the expression Follow these guidelines when using pound signs:
The following sections provide more details on how pound signs should be used in CFML. Pound signs in cfoutput tags
Expressions that contain one variable or a single function can be used freely inside <cfoutput> Value is #Form.MyTextField# </cfoutput> <cfoutput> The name is #FirstName# #LastName#. </cfoutput> <cfoutput> Cos(0) is #Cos(0)# </cfoutput>
If pounds are omitted, the expression text rather than the expression value appears in the output generated by the Two expressions inside pound signs can be adjacent to one another, as in this example: <cfoutput> "Mo" and "nk" is #Left("Moon", 2)# #Mid("Monkey", 3, 2)#
</cfoutput>
Complex expressions and cfoutput tags
Complex expressions, which contain more that one variable or function, cannot be inserted inside <cfoutput>1 + 1 is #1 + 1#</cfoutput>
To insert the value of a complex expression in the output generated by a <cfset Result = 1 + 1> <cfoutput>1 + 1 is #Result#</cfoutput> Pound signs in stringsExpressions that contain one variable or function can be used inside strings if 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 TheString = "Hello, #FirstName#!"> <cfset TheString = "Hello, " & FirstName & "!">
If pound signs are omitted, the expression text, rather than the expression value, appears in the string. For example, the following pairs of <cfset TheString = "Hello, FirstName!"> <cfset TheString = "Hello, " & "First" & "Name!">
As with the <cfset TheString = "Monk is #Left("Moon", 2)##Mid("Monkey", 3, 2)#">
The double quotes around "Moon" and "Monkey" need not be escaped (as in ""Moon"" and ""Monkey""). This is because the text between the pound signs is treated as an expression, it is evaluated first before its value is inserted inside the string. Inserting complex expressions in stringsComplex expressions that have one or more operators cannot be used 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, do one of the following:
Both techniques are shown in the following example; <cfset Result = 1 + 1> <cfset TheString = "1 + 1 is #Result#"> <cfset TheString = "1 + 1 is " & (1 + 1)> To insert the pound character in a string, use two pound signs, as shown below: <cfset TheString = "This is a pound sign ##."> Pound signs in tag attribute valuesThe rules for using pound signs inside strings also 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 Pound signs in custom tag attribute valuesThe rules described in the previous section apply to custom tags. However, custom tags can also have complex objects passed as attribute values, which ColdFusion tags cannot. Complex objects, such as arrays, structures, queries, and COM objects, are passed to custom tags surrounded by pound signs (#). For example: <cfset myinfo = StructNew()> <CF_FillInMyInfo info = #myinfo# name = "pete" size = "22"> Nested pound signsThere are few cases in which pound signs can be nested in an expression. The following example shows a valid use of nested pound signs: <cfset Sentence = "The length of the full name is #Len("#FirstName# #LastName#")#">
Pound signs must be nested so that the values of the variables FirstName and LastName are inserted in the string whose length the <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)#)#">
These statements result in errors. As a general rule, never put pound signs around function arguments. Pound signs in general expressionsMacromedia recommends that you use pound signs only when necessary. The following example shows the preferred method for referencing variables. <cfset SomeVar = Var1 + Max(Var2, 10 * Var3) + Var4> In contrast, note the following example, which uses pound signs unnecessarily: <cfset #SomeVar# = #Var1# + #Max(Var2, 10 * Var3)# + #Var4#>
|