Search code examples
coldfusioncoldfusion-8

Can I use another cfswitch statement within the same cfswitch case


Can I use another cfswitch statement within the same cfswitch case?

<CFSWITCH EXPRESSION="nameOfPg">
  <CFCASE VALUE="ClassMenu" >
    <!---do something--->
  </CFCASE>
  <CFCASE VALUE="ReportsMenu">
    <CFSWITCH EXPRESSION="#nameOfPg#">
      <CFCASE VALUE="StudentMenu">
        <!---do something--->
      </CFCASE>
      <CFCASE VALUE="DetailsMenu">
        <!---do something--->
      </CFCASE>
    </CFSWITCH>
  </CFCASE>
  <CFDEFAULTCASE>
    <!---do something--->
  </CFDEFAULTCASE>
</CFSWITCH>

Solution

  • Yes, you can. The EXPRESSION attribute can be dynamic (e.g. a variable such as nameOfPg), but pay attention to the hashes #.

    nameOfPg (your top CFSWITCH) evaluates the string, not the actual value of the variable nameOfPg. To resolve a variable in an attribute, you need to use hashes # like you did in your inner CFSWITCH: #nameOfPg#.

    Fix your code:

    <CFSWITCH EXPRESSION="#nameOfPg#">
      <CFCASE VALUE="ClassMenu" >
        <!---do something--->
    

    and you should be good.

    On a side note: CFCASE on the other hand does not allow the use of dynamic values (due to to the way a switch works in Java/ColdFusion). You always have to use a static value here like you do right now. Just keep that in mind.