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:
Tags |
Purpose |
---|---|
cfif, cfelseif, cfelse |
Select sections of code based on whether expressions are True or False. |
cfswitch, cfcase, cfdefaultcase |
Select among sections of code based on the value of an expression. Case processing is not limited to True and False conditions. |
cfloop, cfbreak |
Loop through code based on any of the following values: entries in a list, keys in a structure or external object, entries in a query column, an index, or the value of a conditional expression. |
cfabort, cfexit |
End processing of a ColdFusion page or custom tag. |
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 Extending ColdFusion Pages with CFML Scripting. For more details on using flow-control tags, see the reference pages for these tags in the CFML Reference.
The cfif, cfelseif, and cfelse tags provide if-then-else conditional processing, as follows:
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 select among different code blocks based on the value of an expression. ColdFusion processes these tags as follows:
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 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 the following types of loops:
Loop type |
Description |
---|---|
Index |
Loops through the body of the tag and increments a counter variable by a specified amount after each loop until the counter reaches a specified value. |
Conditional |
Checks a condition and runs the body of the tag if the condition is True. |
Query |
Loops through the body of the tag once for each row in a query. |
List, file, or array |
Loops through the body of the tag once for each entry in a list, each line in a file, or each item in an array. |
Collection |
Loops through the body of the tag once for each key in a ColdFusion structure or item in a COM/DCOM object. |
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>
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 and the CFML Reference.