0

How can I check multiple if conditions in twig? Neither of these seem to work Also they all seem a but messy and heavy.

  {% if pageClass != "page-home" %}
  {% if bodyClass != "500" %}
  {% if bodyClass != "404" %}
         {% include '_components/type-bg' with {
                    content: {
                        slug: entry.slug|split(' ')|slice(0, 1)|join
                    },
                } only %}
   {% endif %}
   {% endif %}
   {% endif %}

I have also tried the below

    {% if (pageClass != "page-home") or (if bodyClass != "500") or (if bodyClass != "404")%}

      {% include '_components/type-bg' with {
                    content: {
                        slug: entry.slug|split(' ')|slice(0, 1)|join
                    },
                } only %}

    {% endif %}
DumbDevGirl42069
  • 891
  • 5
  • 18
  • 47
  • 1
    Possible duplicate of [twig: IF with multiple conditions](https://stackoverflow.com/questions/8388537/twig-if-with-multiple-conditions) – LeKhan9 Oct 18 '18 at 03:14
  • @LeKhan9 Nope. My examples above show that I've tried that solution and it doesn't work – DumbDevGirl42069 Oct 18 '18 at 03:43
  • @Jessica Shouldn't it be `and` instead of `or`? – Iwan Wijaya Oct 18 '18 at 03:55
  • @Iwan-Wijaya I want check if ANY of these conditions are true. If any of them are true then include this template with the content. A page can't be 500 404 and page-home :-/ – DumbDevGirl42069 Oct 18 '18 at 04:10
  • 2
    Oh I didn't read the values, I assumed the logic in your first code was correct.. I think it should be `{% if pageClass != "page-home" or bodyClass != "500" or bodyClass != "404 %}` ? – Iwan Wijaya Oct 18 '18 at 04:19
  • @DarkBee - my saviour! How does this work? Because the pages with bodyClass of 500 and 404 are including the template 'type-bg' - could you explain like i'm 5 so I understand and can fix it please? – DumbDevGirl42069 Oct 18 '18 at 04:47

2 Answers2

4

You need to use and as your all condition need to satisfy to execute that code so it must be and

{% if pageClass != "page-home" and bodyClass != "500" and bodyClass != "404" %} 
     {% include '_components/type-bg' with {
         content: {
             slug: entry.slug|split(' ')|slice(0, 1)|join
         },
     } only %}   
{% endif %}

Better solution would be from code block just use PHP code [you can do all PHP stuff here] to achieve this using switch case or etc .. and pass only flag like needToInclude as boolean to view and just use that.

enter image description here

{% if needToInclude %} 
     {% include '_components/type-bg' with {
         content: {
             slug: entry.slug|split(' ')|slice(0, 1)|join
         },
     } only %}   
{% endif %}

if any doubt please comment

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • 1
    Still need more coffee I guess - anyway +1 for doing it on the `PHP` side anyway. I'd personally would add a custom function to `twig` if I need this check more than once though – DarkBee Oct 18 '18 at 08:02
  • yah, but seems according to user/her experience i guess `code block php code` can help her, adding custom function will be overwhelming for user :) , but yes i agree for multiple uses `function` makes much more sense. – Hardik Satasiya Oct 18 '18 at 08:05
2

The correct check would be the following

{% if pageClass != 'page-home' and bodyClass not in [ 500, 404, ] %}

Meaning execute when the pageClass is not the home page and make sure the bodyClass is not an erroneous state

DarkBee
  • 16,592
  • 6
  • 46
  • 58