Multiple Comparisons: CFSWITCH and CFCASE ColdFusion Help

Sometimes even a series of CFIF and CFELSEIF tags is too much to code and read and introduces inefficiencies of its own. For instance consider the situation in which a variable is t~ted against every number from 1 to 10 and a different action is taken accordingly in each Instance

Using CFIF and CFELSEIF, the result looks something like this:

<CFIF TestValue is 1>
Action 1
<CFELSEIF TestValue is 2>
Action 2
<CFELSEIF TestValue is 3>
Action 3
<CFELSEIF TestValue is 4>
Action 4
<CFELSEIF TestValue is 5>
Action 5
<CFELSEIF TestValue is 6>
Action 6
<CFELSEIF TestValue is 7>
Action 7
<CFELSEIF TestValue is 8>
Action 8 .
(CFELSEIF TestValue is 9>
Action 9
<CFELSEIF TestValue is 10>
Action 10
</CFI!=>

This seems simple and straightforward, but it is inefficient. What happens when Test Value is to? The answer is quite simple: AUto conditions must be tested and rejected individually. Also the code is less than ideal a complete separate condition needs to be written for each value being tested.

Using CFSWITCHand CFCASE it is possible to test a single expression against multiple possible values and to take a specific action for each value without writing multiple conditions and without causing all conditions to be tested, only to find the last one was a match.

The two tags CFSWITCHand CFCASE are tied together; they are not used individually. CFSWITCHspecifies the expression that is being tested (in other words, the value that will be used to cause the template to switch between possible actions). CFCASEspecifies each set of actions that occur when the tested expression matches specified values.

The CFSWITCHtag takes a single attribute-EXPRESSION-that specifies the expression being tested. For instance, in our preceding example, the expression is simply TestVa 1ue , so the CFSWITCHtag would read as follows:

<CFSWITCH EXPRESSIOH-‘TestValue’)
CFCASEis then used to test for possible values of the expression, TestVa 1ue. The CFCASEtag must always include the VALUEattribute, which specifies the value against which to test the expression in the CFSWITCHstatement. Putting this together, our series of CFlF and CFELSEIFstatements becomes the following CFSWITCH/CFCASE construct
<CFSWITCH EXPRESSIOH-“TestValue#’)
<CFCASE VALUE-‘l’)
Action 1
</CFCASE>
<CFCASE VALUE-‘2’)
Act~on 2
</CFCASE>
<CFCASE VALUE-‘3’)
_ Action 3
</CFCASE>
<CFCASE VALUE-‘4’)
Action 4
</CFCASE>
<CFCASE VALUE-‘S’)
Action 5
</CFCASE>
<CFCASE VALUE-‘6’)
Action 6
</CF~E>
<CFCASE VALUE-‘7’)
Action 7
</CFCASE>
<CFCASE VALUE-‘S’)
Action 8
</CFCASE>
<CFCASE VALUE-‘9′>
Action 9
</CFCASE>
<CFCASE VALUE-’10’>
Action 10
</CFCASE>
</CFSWITCH>

A useful feature of the switch-case structure is the ability to specify a default action that is, an action to take if the expression fails to match any of the values specified by the CFCASE statements. This is achieved by using the CFDEFAUL TCASE tag which does not take attributes and encloses the default action.

In our example, you may want to specify an action to take when Test Value is not between one and 10:

<CFSWITCH EXPRESSION-‘#TestValue#’>
<CFCASE VALUE-‘l’)
. Action 1
</CFCASE>
<CFCASE VALUE-‘Z’>
Action Z
</CFCASE>
<CFCASE VALUE-‘3’>
Action 3
</CFCASE>
<CFCASE VALUE-‘4’>
Action 4
</CFCASE>
<CFCASE VALUE-‘S’>
Action 5
</CFCASE>
<CFCASE VALUE-‘6’>
Action 6
</CF~E>
<CFCASE VALUE-‘7’>
Action 7
</CFCASE>
<CFCASE VALUE-‘S’>
Action S
</CFCASE>
<CFCASE VALUE-‘9′>
Action 9
</CFCASE>
<CFCASE VALUE-’10’>
Action 10
</CFCASE>
<CFDEFAULTCASE>
DefaL!lt Action
</CFDEFAULTCASE>
</CFSWITCH

Another important feature of the CFCASE tag is that it can be used to test for multiple . values and to perform a single action if the expression matches any of the values. This is done by providing a list of values delimited by a separator such as a comma (which is the default delimiter). For instance, you may want to take the same action of outputting the , value contained in TestVa 1ue when it is between 1 and 5 and otherwise take a default value. This can be done with one CFCASE tag and one CFDEFAULTCASE tag:

‘<CFSWITCH EXPRESSION-‘ #TestVa1uet ‘,>
<CFCASE VALUE-‘l,2,3,4,S’>
<CFOUTPUT><H3>The value is fTestValuef</H3></CFOUTPUT>
</CFCASE>
,<CFD,EFAULTCASE>
<H3>The value f s out of range</H3>
.</CFDEFAULTCASE>
</CFSWITCH>
If you need to use a delimiter other than a comma, then you need to use the DELIMIT –
ERSattribute of the CFCASE tag to indicate the delimiter that you have used:
<CFSWITCH EXPRESSION-‘TestValue’>
<CFCASE VALUE-‘l:2:3:4:S’ DELIMITERS-‘:’>
<CFOUTPUT><H3>The value is fTestValuef</H3></CFOUTPUT>
</CFCASE>
<CFDEFAULTCASE>
<H3>The value is out of range</H3>
</CFDEFAULTCASE>
</CFSWQ:TCH>

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

Share the Story

Back to Top
Share This