如何检查树枝中的多个 if 条件?

How can I check multiple if conditions in twig?

如何检查 twig 中的多个 if 条件? 这些似乎都不起作用 而且它们看起来都很凌乱和沉重。

  {% 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 %}

下面的我也试过了

    {% 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 %}

您需要使用 and 作为您的 all condition need to satisfyexecute that code 所以它必须是 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.

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

如有疑问请评论

正确的检查如下

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

意思是当pageClass不是主页时执行,并确保bodyClass不是错误状态