Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion cfcase statements and referencing their variables?

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.

like image 222
JTester Avatar asked Jul 14 '14 01:07

JTester


3 Answers

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>
like image 146
Dan Short Avatar answered Nov 18 '22 06:11

Dan Short


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>
like image 23
Adam Cameron Avatar answered Nov 18 '22 06:11

Adam Cameron


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

like image 2
James A Mohler Avatar answered Nov 18 '22 06:11

James A Mohler