|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF101
Expressing Yourself in ColdFusion
Using expressions to process data
By: Jeffry Houser
Digg This!
Hello and welcome to the second installment of CF101. Last month I talked about variables in ColdFusion and how to create them using the <cfset> tag. I also showed you how to display the value of a variable using the <cfoutput> tag and a ColdFusion expression. I decided to leave an in-depth discussion of ColdFusion expressions to another time. That time is now. Expressions Explained The simplest expression is a single operand. There are many different types of operands that can be used in a ColdFusion expression. Here are some of the different elements: Let's look at an example to see how expressions are used. Putting an Expression to Use <cfset variableName = value> However, this is actually a simplified version. The value portion of the tag is actually an expression. The following would be a more accurate description of the <cfset> tag: <cfset variableName = expression> Look at the following examples: <cfset FirstName = "Jeffry"> Four variables are created with this segment of code. The first two lines create variables, FirstName and LastName, that contain string values. The value of each variable is a literal expression, with the value being a string. The third line creates a variable called Age using a numeric literal expression. The fourth line creates a variable called FName. Here our expression is a variable, not a literal value. What can be done with the variables once they are created? The simplest example is to display them, using the <cfoutput> tag. <cfoutput> The code starts with the <cfoutput> tag. Then comes an expression that displays the value in the FirstName variable. The expression is followed by an HTML line break. Then comes an expression that displays the LastName variable, and another line break. Third, an expression displays the age variable. Are you noticing a pattern yet? The <cfoutput> tag block contains ColdFusion expressions and HTML. The pound signs allow the CFML server to tell the difference between expressions and plain text or HTML. The expressions are evaluated and the results are returned to the browser. The resulting browser display will look something like this: Jeffry Both code segments will have to be put in the same template to correctly display the output. If you've had any experience with ColdFusion before reading this article, you have probably already been exposed to expressions. Pounding the CFML Pavement <cfset #FirstName# = #"Jeffry"#> However, it is generally not considered good practice to do so. Specifying the pound signs means that your CF Server has to do more work, thus resulting in code that is less efficient than it could be. All those pound signs can also make the code more confusing to read. You don't usually need to use the pound sign if your expression is inside a ColdFusion tag or function. So when do you? A good rule of thumb is that if you're inside quotes you probably need pound signs, otherwise you probably don't (there are a few exceptions to this rule - like isDefined(), which takes a variable name as a string inside quotes). So, if the pound signs aren't needed inside ColdFusion tags or functions, why are they needed in the <cfoutput> tag block? In <cfset> you're using the expression as part of the tag and it is safe to assume you are creating ColdFusion expressions. In the <cfoutput> block the expression is not part of the tag, only part of the text between the open and close tag. ColdFusion needs a way to distinguish between the two, and that is the reason for the pound signs. In most cases you can use pound signs only around simple expressions. To output the value of a complex expression, store the result to a variable and output the variable using a simple expression. If you are creating a dynamic variable name using <cfset>, you'll need to use pound signs. Dynamic variable naming is beyond the scope of this article, but more information can be found at http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/exprea24.htm. You may also need to use pound signs if you want to display the value of a variable inside a string literal (which is when you're inside quotes). More information on this can be found at http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/exprea16.htm. You specify the attributes of many CFML tags as strings. When using them, I find it a good practice to ask myself whether I'm referencing a variable or a string. As mentioned earlier, if it's a variable, then use pound signs. Otherwise, specify the literal value without pound signs. Using Operators to Create Complex Expressions Expression Operator Expression It is worth noting that each expression can contain another expression, thus creating very complex expressions. Operators fall into four main types: arithmetic, strings, decision, and Boolean. Arithmetic operators are used to perform math functions; string operators are used to combine multiple strings into a single string; decision operators are used to produce Boolean values based on the results of a comparison; and Boolean operators are used to perform logical operations. I'll save a discussion of Boolean and decision operators for the next column. Here are some of the more commonly used string and arithmetic operators:
Examples of Complex Expressions <cfset FirstName = "Jeffry"> The code starts out simply by setting two variables, FirstName and LastName. Both of these lines use simple expressions. The next line is more interesting. It creates a variable called FName by concatenating the FirstName variable with the LastName variable. The final piece of code uses the addition operator to add two numbers together. We can display these values using this code: <cfoutput> The display code should be familiar to you based on our other examples. We have a <cfoutput> block and a mix of CFML expressions and HTML line breaks. If you put both code segments into a browser and execute the code, you should see something like this: JeffryHouser But wait. Maybe you don't want to butt the FirstName and LastName up against each other. We can use a string literal and the concatenation operator to modify the <cfset> tag to add a space between the two in the FName value. Replace the third line of cfset code with this: <cfset FName = FirstName & " " & LastName> Then when you execute the display code you will see the full name correctly separated. Conclusion CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||