JTester
JTester

Reputation: 503

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.

Upvotes: 5

Views: 835

Answers (3)

Dan Short
Dan Short

Reputation: 9616

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>

Upvotes: 9

Adam Cameron
Adam Cameron

Reputation: 29870

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>

Upvotes: 6

James A Mohler
James A Mohler

Reputation: 11120

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

Upvotes: 2

Related Questions