ColdFusion provides several tags that let you control how a page gets executed. These tags generally correspond to programming language flow control statements, such as if, then, and else. The following tags provide ColdFusion flow control.
This section provides a basic introduction to using flow-control tags. CFScript also provides a set of flow-control statements. For information on using flow-control statements in CFScript, see Chapter 6, "Extending ColdFusion Pages with CFML Scripting". For more details on using flow-control tags, see the reference pages for these tags in CFML Reference.
The cfif
, cfelseif
, and cfelse
tags provide if-then-else conditional processing, as follows:
cfif
tag tests a condition and executes its body if the condition is True.
cfif
(or cfelseif
) test condition is False, the cfelseif
tag tests another condition and executes its body if that condition is True.
cfelse
tag can optionally follow a cfif
tag and zero or more cfelseif
tags. Its body executes if all the preceding tags' test conditions are False.
The following example shows the use of the cfif
, cfelseif
, and cfelse
tags. If the value of the type variable is "Date," the date displays; if the value is "Time," the time; displays otherwise, both the time and date display.
<cfif type IS "Date">
<cfoutput>#DateFormat(Now())#</cfoutput> <cfelseif type IS "Time"> <cfoutput>#TimeFormat(Now())#</cfoutput> <cfelse> <cfoutput>#TimeFormat(Now())#, #DateFormat(Now())#</cfoutput> </cfif>
The cfswitch
, cfcase
, and cfdefaultcase
tags let you to select among different code blocks based on the value of an expression. ColdFusion processes these tags as as follows:
cfswitch
tag evaluates an expression. The cfswitch
tag body contains one or more cfcase
tags and optionally includes cfdefaultcase
tag.
cfcase
tag in the cfswitch
tag body specifies a value or set of values. If a value matches the value determined by the expression in the cfswitch
tag, ColdFusion runs the code in the body of the cfcase
tag and then exits the cfswitch
tag. If two cfcase
tags have the same condition, ColdFusion generates an error.
cfcase
tags match the value determined by the cfswitch
tag, and the cfswitch
tag body includes a cfdefaultcase
tag, ColdFusion runs the code in the cfdefaultcase
tag body.Note: Although the cfdefaultcase
tag does not have to follow all cfcase
tags, it is good programming practice to put it at the end of the cfswitch
statement.
The cfswitch
tag provides better performance than a cfif
tag with multiple cfelseif
tags, and is easier to read. Switch processing is commonly used when different actions are required based on a a string variable such as a month or request identifier.
The following example shows switch processing:
<cfoutput query = "GetEmployees">
<cfswitch expression = #Department#> <cfcase value = "Sales"> #FirstName# #LastName# is in <b>Sales</b><br><br> </cfcase> <cfcase value = "Accounting"> #FirstName# #LastName# is in <b>Accounting</b><br><br> </cfcase> <cfcase value = "Administration"> #FirstName# #LastName# is in <b>Administration</b><br><br> </cfcase> <cfdefaultcase>#FirstName# #LastName# is not in Sales, Accounting, or Administration.<br> </cfdefaultcase> </cfswitch> </cfoutput>
The cfloop
tag loops through the tag body zero or more times based on a condition specified by the tag attributes. The cfbreak
tag exits a cfloop
tag.
The cfloop
tag provides five types of loops:
The following example shows a simple index loop:
<cfloop index = "LoopCount" from = 1 to = 5>
The loop index is <cfoutput>#LoopCount#</cfoutput>.<br> </cfloop>
The following example shows a simple conditional loop. The code does the following:
<cfset myArray = ArrayNew(1)>
<!--- Use ArraySet to initialize the first ten elements to 123 ---> <cfset ArraySet(myArray, 1, 10, 123)> <cfset myArray[4] = "kumquats"> <cfset foundit = False> <cfset i = 0> <cfloop condition = "(NOT foundit) AND (i LT ArrayLen(myArray))"> <cfset i = i + 1> <cfif myArray[i] IS "kumquats"> <cfset foundit = True> </cfif> </cfloop> <cfoutput> i is #i#<br> foundit is #foundit#<br> </cfoutput>
Note: You can get an infinite conditional loop if you do not force an end condition. In this example, the loop is infinite if you omit the <cfset i = i + 1>
statement. To end an infinite loop, stop the ColdFusion application server.
The cfbreak
tag exits the cfloop
tag. You typically use it in a cfif
tag to exit the loop if a particular condition occurs. The following example shows the use of a cfbreak
tag in a query loop:
<cfloop query="fruitOrder">
<cfif fruit IS "kumquat"> <cfoutput>You cannot order kumquats!<br></cfoutput> <cfbreak> </cfif> <cfoutput>You have ordered #quantity# #fruit#.<br></cfoutput> </cfloop>
The cfabort
tag stops processing of the current page at the location of the cfabort
tag. ColdFusion returns to the user or calling tag everything that was processed before the cfabort
tag. You can optionally specify an error message to display. You can use the cfabort
tag as the body of a cfif
tag to stop processing a page when a condition, typically an error, occurs.
The cfexit
tag controls the processing of a custom tag, and can only be used in ColdFusion custom tags. For more information see, "Terminating tag execution," in Chapter 10 and CFML Reference.