[thelist] ColdFusion: Nested pound signs
judah at wiredotter.com
judah at wiredotter.com
Tue Nov 13 17:09:21 CST 2001
At 04:46 PM 11/13/2001 -0600, you wrote:
>I mean, nesting pound signs, as in:
><CFSET Sentence = "The length of the full name is #Len("#FirstName#
>#LastName#")#">
>
>Look at the bottom of this page:
>http://www.advances.com/cfdocs/lang/lr010005.htm
They tell you the correct information near the bottom of the
page: basically, never nest pound signs. As an interpreted language, CF
parses a document looking for things it can identify as a variable, and
then converting it to its value. As a general rule, if a string is inside
of a CF tag (such as cfset), the string will be treated as a variable
unless specifically demarcated as a string by encasing it in either single
or double quotes.
Example:
><CFSET Sentence = "The length of the full name is #Len("#FirstName#
>#LastName#")#">
Can be written as <cfset sentence = 'The length of the full name is ' &
Len(FirstName&' '&LastName)>
In this case, the single quotes around The length of the full name is tells
CF that its a string and should not be evaluated. The ampersand is a
concatenation operand that sticks the final evaluated product of the right
hand side onto the string on the left hand side.
Since Len is inside a cfset and not inside of quotes, CF checks it and sees
that its a function. Since FirstName and LastName are inside a Len
function and not enclosed in quotes, they are treated as variables. And
the &' '& concatenates the evaluated values of FirstName and LastName with
a string literal space stuck in between.
Pretty much the only time you need to wrap something in pound signs is when
you want to output the contents of a variable that resides within a mass of
text, such as:
<cfset myname = "Judah">
<cfoutput>
Hi, my name is #myname#. I like bananas. Eek, Eek.
</cfoutput>
The only times I can think of that you need pound signs with a CF tag are
when you are using a variable while looping over a list or a structure.
<cfset mystuff = "one,two,three,four">
<cfloop index="stuff" list="#mystuff#">
<cfoutput>#stuff#<br></cfoutput>
</cfloop>
I hope this helps,
Judah
More information about the thelist
mailing list