<p>This currently isn't a supported syntax for the Twig switch statement.</p>

<p>Craft will take that line and compile it down to:</p>

<pre><code>case ("A1" || "A2"):
</code></pre>

<p>Which will always evaluate to <code>true</code>, which is why you always get the first result.</p>

<p>In order for it to work, it would need to be either:</p>

<pre><code>case "A1":
case "A2":
</code></pre>

<p>To take advantage of PHP's fall-through support or the more ugly:</p>

<pre><code>case ($context["bodyClass"] == "A1" || $context["bodyClass"] == "A2")
</code></pre>

CC BY-SA 3.0
2015-08-20T17:32:27.140Z
Yeah I tried it just before your answer by intuition and it worked ! Cool :D
migmaker
CC BY-SA 3.0
2015-08-21T18:54:14.900Z
Hey Brad I think I got a Craft bug here : now my template only show first case (A or C), even when it is false.
migmaker
CC BY-SA 3.0
2015-08-21T19:07:07.653Z
It worked yesterday, but today it doesn't?
Brad Bell
CC BY-SA 3.0
2015-08-21T19:24:14.530Z
Because I just checked `case A or C` yesterday. I did not check the other cases. Thats why I did not see it... I really double checked the value of `bodyclass` before the switch. And it displays B. But the switch return `A or C` case.
migmaker
CC BY-SA 3.0
2015-08-21T19:37:26.307Z
Not sure I'm following... what's the value of `bodyClass` that causes B to be displayed?
Brad Bell
CC BY-SA 3.0
2015-08-21T20:08:19.220Z
Sorry Brad my english is tired :) I tried to rephrase above in my main question, below EDIT CRAFT BUG?
migmaker
CC BY-SA 3.0
2015-08-21T20:22:45.897Z
Ahh, that does look like a bug, then.  Will look into it.
Brad Bell
CC BY-SA 3.0
2015-08-21T22:04:44.020Z
Looks like I'm going to have to completely change my answer on this. :)
Brad Bell
CC BY-SA 3.0
2015-08-24T03:48:59.070Z
@BradBell: I don't think the Craft switch tag supports the php fall through. Pretty sure you get a `break;` for free with every `case`.
Marion Newlevant
CC BY-SA 3.0
2015-08-31T19:24:12.967Z
Sorry brad I come back here, you mean I must avoir using switch here ? This syntax `"A1": case "A2":`  is Craft switch case syntax ? no ?
migmaker
CC BY-SA 3.0
2015-08-31T19:25:35.830Z
@migswd Yeah, Craft's Twig switch statement won't work for this.
Brad Bell
CC BY-SA 3.0
2015-08-20T17:28:53.900Z
2015-08-28T19:13:49.063Z
2015-08-28T19:13:49.063Z
Brad Bell
<p>The <code>switch</code> tag is a Twig extension that comes as part of Craft. From looking at the code for the <code>Switch_TokenParser</code> in the Craft <a href="https://buildwithcraft.com/classreference/etc/templating/twigextensions/Switch_TokenParser" rel="nofollow">full class reference</a> it looks as if it is expecting an expression after <code>case</code> so you should be able to put in any valid Twig expression that equates to <code>true</code> or <code>false</code>.</p>

CC BY-SA 3.0
2015-08-20T15:46:57.630Z
2015-08-20T15:46:57.630Z
Steve Rowling
<p>I got a Twig <code>switch</code> in a template like:</p>

<pre><code>{% switch bodyClass %}

    {% case 'A' %}
        ...

    {% case 'B' %}
        ...

    {% default %}
        ...

{% endswitch %}
</code></pre>

<p>I would like to add another possible value for the first <code>case</code>.<br>
So I would have <code>case 'A' or 'C'</code>.  </p>

<p>Is it possible in Twig syntax?</p>

<p>=================================<br>
<strong>EDIT CRAFT BUG ?</strong><br>
OK lets try to rephrase, sorry for my english not easy to explain for me sometimes :)<br>
So when the <code>or</code> syntax is added, then the 2nd and 3rd cases <code>B</code> or <code>default</code> (after <code>'A1' or 'A2'</code> case) are never reached anymore.<br>
The switch always say <code>TRUE</code> to 1st case (even if it is wrong...).</p>

<pre><code>{% switch bodyClass %}

    {% case 'A1' or 'A2' %}
        ...

    {% case 'B' %}
        ...

    {% default %}
        ...

{% endswitch %}
</code></pre>

<templating><switch>
Switch Twig condition Case with OR condition (two values in the case)
# Switch Twig condition Case with OR condition (two values in the case)

(Asked by: username_0 on 2015-08-20)

I got a Twig switch in a template like:
{% switch bodyClass %}

    {% case 'A' %}
        ...

    {% case 'B' %}
        ...

    {% default %}
        ...

{% endswitch %}

I would like to add another possible value for the first case.
So I would have case 'A' or 'C'.  
Is it possible in Twig syntax?
=================================
EDIT CRAFT BUG ?
OK lets try to rephrase, sorry for my english not easy to explain for me sometimes :)
So when the or syntax is added, then the 2nd and 3rd cases B or default (after 'A1' or 'A2' case) are never reached anymore.
The switch always say TRUE to 1st case (even if it is wrong...).
{% switch bodyClass %}

    {% case 'A1' or 'A2' %}
        ...

    {% case 'B' %}
        ...

    {% default %}
        ...

{% endswitch %}

**Tags:** templating, switch


---

## 2 answers

### Answer by username_1 on 2015-08-20. Score: 3 | Accepted answer

This currently isn't a supported syntax for the Twig switch statement.
Craft will take that line and compile it down to:
case ("A1" || "A2"):

Which will always evaluate to true, which is why you always get the first result.
In order for it to work, it would need to be either:
case "A1":
case "A2":

To take advantage of PHP's fall-through support or the more ugly:
case ($context["bodyClass"] == "A1" || $context["bodyClass"] == "A2")

> **Comments:**
> **username_0**: Yeah I tried it just before your answer by intuition and it worked ! Cool :D  
> **username_0**: Hey Brad I think I got a Craft bug here : now my template only show first case (A or C), even when it is false.  
> **username_1**: It worked yesterday, but today it doesn't?  
> **username_0**: Because I just checked `case A or C` yesterday. I did not check the other cases. Thats why I did not see it... I really double checked the value of `bodyclass` before the switch. And it displays B. But the switch return `A or C` case.  
> **username_1**: Not sure I'm following... what's the value of `bodyClass` that causes B to be displayed?  
> **username_0**: Sorry Brad my english is tired :) I tried to rephrase above in my main question, below EDIT CRAFT BUG?  
> **username_1**: Ahh, that does look like a bug, then.  Will look into it.  
> **username_1**: Looks like I'm going to have to completely change my answer on this. :)  
> **username_2**: @BradBell: I don't think the Craft switch tag supports the php fall through. Pretty sure you get a `break;` for free with every `case`.  
> **username_0**: Sorry brad I come back here, you mean I must avoir using switch here ? This syntax `"A1": case "A2":`  is Craft switch case syntax ? no ?  
> **username_1**: @migswd Yeah, Craft's Twig switch statement won't work for this.  

---

### Answer by username_3 on 2015-08-20. Score: 1

The `switch` tag is a Twig extension that comes as part of Craft. From looking at the code for the `Switch_TokenParser` in the Craft [full class reference](https://buildwithcraft.com/classreference/etc/templating/twigextensions/Switch_TokenParser) it looks as if it is expecting an expression after `case` so you should be able to put in any valid Twig expression that equates to `true` or `false`.


---

