Using pound signs

Pound signs (#) have a special meaning in CFML. When the ColdFusion Server encounters pound signs in CFML text, such as the text in a cfoutput tag body, it checks to see if the text between the pound signs is either a variable or a function.

Is so, it replaces the text and surrounding pound signs with the variable value or the result of the function. Otherwise, ColdFusion generates an error.

For example, to output the current value of a variable named Form.MyFormVariable, you delimit (surround) the variable name with pound signs:

<cfoutput>Value is #Form.MyFormVariable#</cfoutput>

In this example, the variable Form.MyFormVariable is replaced with the value assigned to it.

Follow these guidelines when using pound signs:

The following sections provide more details on how to use pound signs in CFML. For a description of using pound signs to create variable names, see "Using pound signs to construct a variable name in assignments"

Using pound signs in ColdFusion tag attribute values

You can put variables, functions, or expressions inside tag attributes by enclosing the variable or expression with pound signs. For example, if the variable CookieValue has the value "MyCookie", the following line sets the cfcookie value attribute to "The value is MyCookie":

<cfcookie name="TestCookie" value="The value is #CookieValue#">

You can optionally omit quotation marks around variables used as attribute values as shown in the following example:

<cfcookie name = TestCookie value = #CookieValue#>

However, surrounding all attribute values in quotation marks is more consistent with HTML coding style.

If you use string expressions to construct an attribute value, as shown in the following example, the strings inside the expression use single quotation marks (') to differentiate the quotation marks from the quotation marks that surround the attribute value.

<cfcookie name="TestCookie2" value="The #CookieValue & 'ate the cookie!'#">

Note:   You do not need to use pound signs when you use the cfset tag to assign one variable's value to another value. For example, the following tag assigns the value of the oldVar variable to the new variable, newVar: <cfset newVar = oldVar>.

Using pound signs in tag bodies

You can put variables or functions freely inside the bodies of the following tags by enclosing each variable or expression with pound signs:

For example:

<cfoutput>
  Value is #Form.MyTextField#
</cfoutput>

<cfoutput>
  The name is #FirstName# #LastName#.
</cfoutput>

<cfoutput>
  The value of Cos(0) is #Cos(0)#
</cfoutput>

If you omit the pound signs, the text, rather than the value, appears in the output generated by the cfoutput statement.

Two expressions inside pound signs can be adjacent to one another, as in the following example:

<cfoutput>
  "Mo" and "nk" is #Left("Moon", 2)##Mid("Monkey", 3, 2)#
</cfoutput>

This code displays the following text:

"Mo" and "nk" is Monk

ColdFusion does not interpret the double pound sign as an escaped # character.

Using pound signs in strings

You can put variables or functions freely inside strings by enclosing each variable or expression with pound signs; for example:

<cfset TheString = "Value is #Form.MyTextField#">
<cfset TheString = "The name is #FirstName# #LastName#.">
<cfset TheString = "Cos(0) is #Cos(0)#">

ColdFusion automatically replaces the 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 omitted inside the string, the text, rather than the value, appears 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 with the cfoutput statement, two expressions can be adjacent to each other in strings, as in the following example:

<cfset TheString = "Monk is #Left("Moon", 2)##Mid("Monkey", 3, 2)#">

The double quotes around "Moon" and "Monkey" do not need to be escaped (as in ""Moon"" and ""Monkey""). This is because the text between the pound signs is treated as an expression; it is evaluated before its value is inserted inside the string.

Nested pound signs

In a few cases, you can nest pound signs in an expression. The following example uses nested pound signs:

<cfset Sentence = "The length of the full name is
#Len("#FirstName# #LastName#")#">

In this example, pound signs are nested so that the values of the variables FirstName and LastName are inserted in the string whose length the Len function calculates.

Nested pound signs imply a complex expression that can typically be written more clearly and efficiently without the nesting. For example, you can rewrite the preceding code example without the nested pound signs, as follows:

<cfset Sentence2 = "The length of the full name is #Len(FirstName & " "
& LastName)#">

The following achieves the same results and can further improve 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)#)#">

These statements result in errors. As a general rule, never put pound signs around function arguments.

Using pound signs in expressions

Use pound signs in expressions only when necessary, because unneeded pound signs reduce clarity and can increase processing time. The following example shows the preferred method for referencing variables:

<cfset SomeVar = Var1 + Max(Var2, 10 * Var3) + Var4>

In contrast, the following example uses pound signs unnecessarily and is less efficient than the previous statement:

<cfset #SomeVar# = #Var1# + #Max(Var2, 10 * Var3)# + #Var4#>

Comments