Nested Pound Signs; Using Pound Signs In Expressions - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

As with the
cfoutput
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
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#>
statement, two expressions can be adjacent to each other in strings, as in
function calculates.
Len
Using pound signs
91

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents