5

If you have code like so:

<cfcase value="Test">
   /**Do Stuff
</cfcase>

Is it possible to reference that value within the case statement?

I want to concatenate a list that can handle multiple cases and be able to dynamically reference the variables like so:

<cfcase value="Test,Another,Yes,No">
   <cfif this.value EQ 'Test'> blabla </cfif>
</cfcase>

I can't find anything that detailed about this for everywhere I have looked, just curious if it is even possible.

JTester
  • 463
  • 1
  • 8
  • 19

3 Answers3

9

Yes, you can run multiple case statement in a cfcase tag:

<cfswitch expression="#URL.TestValue#">

    <cfcase value="Blue,Red,Orange" delimiters=",">
        <cfoutput>#URL.TestValue#</cfoutput>
    </cfcase>

    <cfcase value="Yellow">
        <cfoutput>#URL.TestValue#</cfoutput>
    </cfcase>

</cfswitch>
Dan Short
  • 9,598
  • 2
  • 28
  • 53
6

Well... yeah... if your <cfswitch> expression was #originalExpression#, then the value that caused the case to trigger will be... #originalExpression#. There's no need to be tricky about it!

IE: you'll need to do something like this:

<cfswitch expression="#originalExpression#">
    <cfcase value="Test,Another,Yes,No">
        <!--- stuff common to all of Test,Another,Yes,No ---->

        <!--- stuff specific to various cases --->
        <cfif originalExpression EQ "test">
            <!--- do stuff ---->
        <cfelseif listFindNoCase("Yes,No", originalExpression)>
            <!--- do stuff ---->
        <cfelse>
            <!--- do stuff for "another" --->
        </cfif>
    </cfcase>
    <!--- other cases etc --->
</cfswitch>
Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
2

I don't think it is possible using ColdFusion tags. You can do something similar with <cfscript>

switch (expression)    {

    case "Test" :
       // Do some extra stuff
       // No break here

    case "Another" : case "Yes" : case "No" :
      // Do yet some normal stuff
      break;
    }

Disclaimer: I would not want to maintain this code

James A Mohler
  • 11,060
  • 15
  • 46
  • 72