View comments | RSS feed

cfswitch

Description

Evaluates a passed expression and passes control to the cfcase tag that matches the expression result. You can, optionally, code a cfdefaultcase tag, which receives control if there is no matching cfcase tag value.

Category

Flow-control tags

Syntax

<cfswitch 
  expression = "expression">
  <cfcase 
    value = "value" 
    delimiters = "delimiters">
    HTML and CFML tags
  </cfcase>
  additional <cfcase></cfcase> tags
  <cfdefaultcase>
    HTML and CFML tags
  </cfdefaultcase>
</cfswitch>

See also

cfabort, cfloop, cfbreak, cfrethrow, cfexecute, cfexit, cfthrow, cfif, cftry, cflocation

History

New in ColdFusion MX: you can put the cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last item.

Attributes

Attribute Req/Opt Default Description
expression
Required

ColdFusion expression that yields a scalar value. ColdFusion converts integers, real numbers, Booleans, and dates to numeric values. For example, True, 1, and 1.0 are all equal.
value
Required

One or more constant values that cfswitch compares to the expression (case-insensitive). If a value matches expression, cfswitch executes the code between cfcase start and end tags.
Duplicate value attributes cause a runtime error.
delimiters
Optional
, (comma)
Character that separates entries in a list of values.

Usage

This tag requires an end tag. All code within this tag must be within a cfcase or cfdefaultcase tag. Otherwise, ColdFusion throws an error.

Use this tag followed by one or more cfcase tags. Optionally, include a cfdefaultcase tag. This tag selects the matching alternative from the cfcase and cfdefaultcase tags, jumps to the matching tag, and executes the code between the cfcase start and end tags. You do not have to explicitly break out of the cfcase tag, as you do in some languages.

You can specify only one cfdefaultcase tag within a cfswitch tag. You can put the cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last item. For example:

<cfset foo=1>
<cfswitch expression="#foo#">
  <cfdefaultcase>
    <cfset fooMore= #foo# + 1>
  </cfdefaultcase> 
  <cfcase value="2">
    <cfset fooMore = #foo# + 2>
  </cfcase>
</cfswitch> 

<cfdump var=#fooMore#> 

The cfswitch tag provides better performance than a series of cfif/cfelseif tags, and the code is easier to read.

Example

cfcase to 
exercise a case statement in CFML --->
<cfquery name = "GetEmployees" dataSource = "cfsnippets">
  SELECT  Emp_ID, FirstName, LastName, EMail, Phone, Department
  FROM   Employees
</cfquery>

<h3>cfswitch Example</h3>
<!--- By outputting the query and using cfswitch, we classify the 
output without using a cfloop construct. --->
<p>Each time the case is fulfilled, the specific information is printed; 
if the case is not fulfilled, the default case is output </p> 
<cfoutput query="GetEmployees"> 
<cfswitch expression="#Trim(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><br>
  </cfdefaultcase> 
</cfswitch> 
</cfoutput> 

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


scjackson said on Jul 15, 2002 at 5:52 PM :
The Docs say....

You can put the cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last item.

This is incorrect. At least on CF5

Generates error...

Context validation error in tag CFDEFAULTCASE

The tag is not correctly positioned relative to other tags in the template: tag CFDEFAULTCASE must be the last child of its parent tag CFSWITCH.
ctina said on Jul 22, 2002 at 2:12 PM :
Update from Macromedia, in response to fusebox_steve:
The parameters are described in the CFSWITCH tag entry. (Click the CFSWITCH link on this page.)
peterson2003 said on Dec 30, 2002 at 2:55 PM :
Dangit! I know there's a way to handle multiple cases within one case, but cannot find an example, e.g.

<case value = "1,2,3,4,5">
do something
</case>

Hey that actually looks right - maybe I'll try it!
psychogears said on Jun 12, 2003 at 10:02 PM :
I don't know if this is a bug, but I had the following code throw an unknown error:

<cfswitch expression="#tSQL#">
<cfcase value="...">
</cfcase>
.
.
.
<cfdefaultcase><!--- do nothing ---></cfdefaultcase>
</cfswitch>

I banged my hands on my computer for two days trying to fix this and finally did a line-by-line comparison with a version of my page that was working. It turns out that once I did this:

<cfswitch expression="#tSQL#">
<cfcase value="...">
</cfcase>
.
.
.
<cfdefaultcase><!--- do nothing ---> </cfdefaultcase>
</cfswitch>

My page was working again. It's a weird error because it only happened when I put in two cfcase blocks which contained cffile and cflock tags, but when I took those two tags out the cfdefaultcase thing didn't happen, so I thought it was a problem with cflock and cffile.

To make a Long-winded story short, moral of the day is put something...anything... between coldfusion tags, even if it's just one whitespace. Apparently CF likes space between tags, and comments don't count. Good luck, I hope no one else runs into this problem.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt310.htm