首页 > 解决方案 > 如何检查树枝中的多个 if 条件?

问题描述

如何检查树枝中的多个 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 %}

标签: symfonytwigoctobercmscraftcms

解决方案


你需要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 %}

更好的解决方案code block只使用 PHP 代码 [你可以在这里做所有 PHP 的东西] 来实现这一点,switch case or etc ..并且只传递像布尔值这样的标志来查看并使用它。needToInclude

在此处输入图像描述

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

如有任何疑问,请发表评论


推荐阅读