switch statement in twig drupal 8
Asked Answered
C

4

14

Does twig in drupal 8 have a switch case statement?

Something like:

{% set size = rows | length %}
{% switch rows %}
    {% case "1" %}
        {{ do something }}
    {% case "2" %}
        {{ do example }}
    {% case "3" %}
        {{ do that }}
    {% default %}
        <p>A font walks into a bar.</p>
        <p>The bartender says, “Hey, we don’t serve your type in here!”</p>
{% endswitch %}

I tried this:

 {% if size ==1 %}
values 1
{% elseif size ==2 %}
values 2
{% else %}
value not found
{% endif %}

But it seems its stucked on the first statement never goes to the second section/statement even when the value is 2

Corfam answered 5/11, 2016 at 11:41 Comment(0)
V
9

I was also looking to do a "switch statement" for my view template for Drupal 8, but I couldn't get it to work. I had the following:

{% set rowsLength = rows|length %}
{% switch rowsLength %}
    {% case 1 %}
        ...
    {% case 2 %}
        ...
    {% case 0 %}
        ...
{% endswitch %}

But when uploaded it just gave didn't render and put at that message of "something is wrong". So I ended up using the following "if" statement:

{% set rowsLength = rows|length %}
{% if rowsLength > 0 and rowsLength < 4  %}
    {% set nav_size = "small-carousel" %}
{% elseif rowsLength > 4 and rowsLength < 6 %}
    {% set nav_size = "medium-carousel" %}
{% else %}
    {% set nav_size = "" %}
{% endif %}

Hope it helps.

Vomit answered 5/11, 2016 at 19:4 Comment(0)
P
21

I think there doesn't exist a Switch function in Symfony/Twig. You have to fall back on a standard

{% if condition %}
...
{% elseif condition2 %}
...
{% else %}
...
{% endif %}

Hope I could help..

Phosphoroscope answered 3/11, 2018 at 16:36 Comment(0)
V
9

I was also looking to do a "switch statement" for my view template for Drupal 8, but I couldn't get it to work. I had the following:

{% set rowsLength = rows|length %}
{% switch rowsLength %}
    {% case 1 %}
        ...
    {% case 2 %}
        ...
    {% case 0 %}
        ...
{% endswitch %}

But when uploaded it just gave didn't render and put at that message of "something is wrong". So I ended up using the following "if" statement:

{% set rowsLength = rows|length %}
{% if rowsLength > 0 and rowsLength < 4  %}
    {% set nav_size = "small-carousel" %}
{% elseif rowsLength > 4 and rowsLength < 6 %}
    {% set nav_size = "medium-carousel" %}
{% else %}
    {% set nav_size = "" %}
{% endif %}

Hope it helps.

Vomit answered 5/11, 2016 at 19:4 Comment(0)
P
0

I too have run into this problem. I was hoping a switch statement would help me to write slightly cleaner code, but I always get a syntax error, unexpected switch tag.

I found the switch statement is documented on some 3rd party websites, but it's not mentioned on the official documentation https://twig.symfony.com/doc/3.x/

The reason for this, the developer of twig wants to keep the tags simple for web designers to use. The functionality of the switch statement is already covered by if elseif else endif https://github.com/twigphp/Twig/pull/185

If you still want to use switch statements in your templates, the functionality can be added with a plugin, such as this one https://packagist.org/packages/buzzingpixel/twig-switch

At the end of the day, it's really not that bad to just use if elseif and so on. It can be written fairly clean:

<button type="button" class="btn {%
if     project.status == 'ok'     %}btn-success{%
elseif project.status == 'active' %}btn-info{%
elseif project.status == 'failed' %}btn-danger{%
else                              %}btn-warning{%
endif
%}">
Peroxidize answered 20/3, 2021 at 12:23 Comment(0)
C
-2

I resolved too by using the if statement

{% set rowsLength = rows|length %}
{% if size == 1 %}
values 1
{% elseif size == 2 %}
values 2
{% else %}
value not found
{% endif %}
Corfam answered 6/11, 2016 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.