cfswitch    cfcase    cfdefaultcase

Description

Used with cfcase and cfdefaultcase. 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    cfelseif    cfelse, cftry cfcatch, cflocation

Attributes

Attribute
Description
expression
Required. Any 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 specified expression (case-insensitive comparison). If a value matches the expression, cfswitch executes the code between the cfcase start and end tags.
Separate multiple values with a comma or other delimiter, as specified in the delimiters parameter. Duplicate value attributes are not allowed and causes a runtime error.
delimiters
Optional. Specifies the character that separates multiple entries in a list of values. The default delimiter is the comma (,).

Usage

Use cfswitch followed by one or more cfcase tags, optionally ending with a cfdefaultcase tag. The cfswitch tag selects the matching alternative from the specified cfcase and cfdefaultcase tags and jumps to the matching tag, executing the code between the cfcase start and end tags. There is no need to explicitly break out of the cfcase tag, as there is in some other languages.

You can specify only one cfdefaultcase tag within a cfswitch tag. cfcase tags cannot appear after the cfdefaultcase tag.

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

Example

<!--- This example illustrates the use of cfswitch and

cfcase to exercise a case statement in CFML --->



<!--- query to get some information --->

<cfquery name = "GetEmployees" dataSource = "cfsnippets">

SELECT  Emp_ID, FirstName, LastName, EMail,

     Phone, Department

FROM   Employees

</cfquery>



<html>

<head>

<title>

cfswitch Example

</title>

</head>



<body bgcolor = silver>

<H3>cfswitch Example</H3>



<!--- By outputting the query and using cfswitch,

we can classify the output without using a cfloop construct.

 --->

<cfoutput query = "GetEmployees">

<cfswitch expression = #Department#>

<!--- each time the case is fulfilled, the specific

information is printed; if the case is not fulfilled,

the default case is output --->

  <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>



</body>

</html>    



Banner.Novgorod.Ru