Controlling The Flow Of Your Templates ColdFusion Help

Until this point all your templates have been designed to execute from start to finish Without applying any decision making that could affect which tags get executed and which don’t

In this chapter you will move into a new realm in your programming flow control. Flow control refers to the ability to determine which portions of a template to execute and in what order based on specific conditions such as the value stored in a variable. Any form of scripting or programming language including ColdFusion, realizes its true power through a range of techniques that can be used to govern which program segments are executed under given conditions.

You Will also look at the most basic flow-control structures commonly found in all scripting environments

• If-Else constructs (using CFIF, CFElSE. and CFElSEIF)
•Switch-case constructs (using CFSWITCH, CFCASE. and CFDEFAUl TCASE)

Another area you will look at, which is related to flow control, is the ability to redirect users to another template using the CFlOCATION tag. Finally, you will look at mechanisms for prematurely ending execution of a template or a segment of code, including the use of the CFABORT and CFEXIT tags.

Displaying Data Based on Conditions

The ability to dictate what actions are performed based on a condition is one of the most commonly used programming tools. All scripting and programming languages offer this capability as does Cold Fusion. In ColdFusion two main constructs handle conditional
decision-making:

• If-Then-Else constructs
• Switch-case constructs

If-Then-Else Decision Making.

The concept of If-Then and If-Then-Else constructs is really quite Simple. Written in plain English a simple If-Then structure takes the following form: “If some condition is true then perform a particular series of actions

Understanding Conditions

The logical question here is: Just what is meant by a condition? Put simply, a condition is any expression that evaluates to a Boolean value (that is, evaluates to either True or False) .

In Chapter 6 “Writing Expressions” you learned how to build expressions in Cold-Fusion. To understand conditional decision making in ColdFusion, you need to start .with a quick review of the operators that enable you to build Boolean expressions

Boolean expressions range from the simple:

• VariableName IS Value
• VariableName GREATERTHANValue
• Variable Name IS NOT Value to the complex:
• Variable One IS Value One AND (Variable Two GREATER THAN Value Two OR Variable Three IS NOT Value Three) .
• NOT(VariableOne IS Value One ANDVariableTwo GREATERTHANValueTwo)
At their most basic, Boolean expressions include any single operator that evaluates to
True or False. These comparison operators generally compare two values, as shown in Table 7.1

TAB L E 7. 1:

TAB L E 7. 1:

These. operators allow the creation of simple Boolean expressions that compare two values. But what happens when you want to build more complex expressions? These needs are addressed by using Boolean operators. Boolean operators work against two other expressions that already return Boolean values and return a new Boolean value as a result. Using these operators, it is.possible to build complex, compound Boolean expressions .

The following list.outlines the Boolean operators available in ColdFusion:

NOT This operator takes a single expression that follows it. The operator returns the negation of its operand. If the expression against which NOT is used is True. then the compound expression returns False. Similarly using NOT against an expression that is False returns True.
AND Returns True when both arguments are True otherwise False is returned. OR Returns True if either argument or both are True.
XOR Returns True if either argument, but not-both, are True.
EQV Returns True if both arguments are True or if both arguments are False.
IMP A IMP Breturns True if the logical statement If A Then B is True. If A is True and B is False, then A IMP B is False; otherwise, A IMP B is True.

Using the CFIF Tag

Now that you know how to build condition expressions you are ready to use the CFIF tag to build a simple If-Then construct. Let’s consider a simple example in which a variable named Price contains a value.

If the value of Price is greater than 10 you want to display a statement to that effect. Otherwise the rest of the template proceeds normally. To do this you use the CFIF tag which takes the following form:’

<CFIF Condition>’
Actions to perform/text to display if the condition is true
</CFIF>
Therefore, our simple example would look like this:
<CFIF Price greater than 10>
<H3>The price is greater than SlO</H3>
. </CFIF>
Carrying things a bit further, you can use a compound Boolean expression to test if the
value in question is between 10 and 20:
<CFIF Price greater than 10 AND Price less than 20>
<H3>The price is between S10 and S20</H3>
</CFIF>

It is important to note that between the opening and closing CFIF tags, any code that is valid in a ColdFusion template can be used. This includes all ColdFusion rctgsas well as all valid HTML. In ColdFusion, CFIF will often be used to control information that is displayed..in the browser, but it can also be used for ColdFusion code and tags that generate no output to the browser.

Adding the CFELSE Tag

Using the CFELSE tag provides a way to extend the basic If-Then structure of the CFIF t2g to include code that handles all cases in which the spedJied condifion is In plain English, using the CFELSE structure works like this: “If some condition is true, then .perform a specific series of actions; otherwise, perform a different series of actions. In ColdFusion, this is done by adding the CFELSE tag in the middle of the CFIF structure:
<CFIF Condition>
-Some Code
<CFELSE>
.Some other Code
</CFIF> .
To see how this works, let’s extend our simple example of testing a variable to see if it
is between 10 and 20:
<CFIF Price greater than 10 and Price less than 20>
<H3>The price is between S10 and S20</H3>
<CFELSE>
<H3>The price is not between S10 and S20</H3>
</CFIF>

Going Further with CFELSEIF

One of the drawbacks of the basic If-Then-Else construct using CFIF and CFELSE is that it can be inefficient. For instance consider  situation in which you need to test a number and output a message based on whether the number is between zero and nine 10 and 19 20 and 29  or 30 and 39

Using CFIF, it is possible to build a simple series of CFIF tags:
<CFIF Price greater than 0 and Price less than 9>
<H3>The price is between SO and S9</H3>
</CFIF> .
<CFIF Price greater than 10 and Price less than 19>
<H3>The price is between S10 and Sl9</H3>
</CFIF>
<CFIF Price greater than 20 and Price less than 29>
<H3>The price is between S20 and S29</H3>
</CFIF>
<CFIF Price greater than 30 and Price less than 39>
<H3>The price is between S30 and S39</H3>
</CFIF>

It should be clear that the code will work. But there is a problem: Even though a value meets one of the earlier conditions (for instance it is between zero and nine) the value will still be tested in the remaining conditions and of course rejected. This c introduce notable inefficiency in long series of CFIF tags or in cases where this type of testing is repeated frequently.

Using the CFELSE tag, it is possible to nest a series of decisions so that less testing occurs:

<CFIF Price greater than 0 and Price less than 9>
<H3>The price is between SO and S9</H3>
<CFElSE>
<CFIF Price greater than 10 and Price less than 19> ~
<H3>The price is between S10 and S19</H3>
<CElSE>
<CFIF Price greater than 20 and Price less than 29>
<H3>The price is be~en S20 and S29</H3>
<CFElSE>
<CFIF Price greater than 30 and Price less than 39>
<H3>The price is between S30 and S39</H3>
</CFIF>
</CFIF>
</CFIF>
</CFIF>

What happens here is that only when the first condition fails does the second condition get tested and then only when the second condition fails is the next condition tested and so on. This addresses the inefficiency issue in the first example. However it produces awkward and hard-to-read code.

Often. though, programmers want to produce a series of conditional tests of the form HThen- Else, H-Then-Else, H-Then-Else, and so on. This is exactly what this series of embedded CFIF and CFELSE tags produces, although with code that is awkward to read and write.

The CFELSEIF tag provides a way to combine a CFELSE tag with the subsequent CFIF condition, producing code of this form:

<CFIF Condition>
Code to Execute
<CFElSEIF other Condition>
Other Code to Execute
<CFElSE>
Some Other Code to Execute
</CFIF>

Applied to our series of consecutive CFIF statements, the resulting code looks like this:

<CFIF Price greater than 0 and Price less than 9>
<H3>The price is between SO and S9</H3>
<CFELSEIF Price greater than 10 and Price less than 19>
<H3>The price is between no and Sl9</H3> ._._.- ..–
<CFELSEIF Price greater than 20 and Price less than 29>
<H3>The price is between S20 and S29</H3>
<CFELSEIF Price greater than 30 and Price less than 39>
.,<H3>The price ‘is b)1;ween S30 and S39</H3>
</CFIF>

‘Not only does this code address the inefficiency issues from the first example it also eliminates the awkward appearance of the code in the second example (making it shorter and easier to read than even the fir~t example of consecutive CFIF tags).

Posted on November 14, 2015 in Controlling The Flow Of Your Templates

Share the Story

Back to Top
Share This