0

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

2 回答 2

4

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

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

于 2018-10-18T07:21:46.197 回答
2

正确的检查如下

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

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

于 2018-10-18T07:44:26.313 回答