Using number signs

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

Note:

Number signs are also called pound signs.

Is so, it replaces the text and surrounding number 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 number 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 number signs:

  • Use number signs to distinguish variables or functions from plain text.
  • Surround only a single variable or function in number signs; for example, #Variables.myVar# or #Left(myString, position)#. (However, a function in number signs can contain nested functions, such as #Left(trim(myString), position)#.
  • Do not place complex expressions, such as{{ 1 + 2 }}in number signs. Although this is allowed in a cfoutput block, such as <cfoutput>One plus one is #1 + 1#</cfoutput>, doing so mixes logic and presentation.
  • Use number signs only where necessary, because unneeded number signs slow processing.
    For a description of using number signs to create variable names, see Using number signs to construct a variable name in assignments in Dynamic expressions and dynamic variables.

Using number signs in ColdFusion tag attribute values

You can place variables, functions, or expressions inside tag attributes by enclosing the variable or expression with number 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 number 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 number signs in tag bodies

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

  • cfoutput
  • cfquery
  • cfmail
    For example:
  • 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 number signs, the text, rather than the value, appears in the output generated by the cfoutput statement.
    Two expressions inside number signs can be adjacent to one another, as in the following example:

    "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 number sign as an escaped # character.

    Using number signs in strings

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

    <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 & "!">

    If number 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, " & "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-quotation marks around "Moon" and "Monkey" do not need to be escaped (as in ""Moon"" and ""Monkey""). This is because the text between the number signs is treated as an expression; it is evaluated before its value is inserted inside the string.

    Nested number signs

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

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

    In this example, number 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 number 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 number 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 Sentence = "The length of the full name is #Len(FullName)#">

    A common mistake is to place number signs around the arguments of functions, as in:

    <cfset ResultText = "#Min(#ThisVariable#, 5 + #ThatVariable#)#">
    <cfset ResultText = "#Len(#Left("Some text", 4)#)#">

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

    Using number signs in expressions

    Use number signs in expressions only when necessary, because unneeded number 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 number signs unnecessarily and is less efficient than the previous statement:

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

    Get help faster and easier

    New user?