如果一个是真的而另一个是假的,那就是树枝

时间:2014-07-29 12:42:54

标签: twig

与此question相关。

当a == true而b == false时,你会怎么做?这一定是 在投票之前是否相信,但没有什么可以找到的。

所以:

{% if a == true and b == false %}
do stuff
{% endif %}

你应该说这应该有效,但事实并非如此:

{% if (a == true) and (b == false) %}
do stuff
{% endif %}

UPDATE2 这是有效的,因为一个是真的,两个是假的

{% if variant.stock.track == true %} 
{% if variant.stock.on_stock == false %}
  ({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
{% endif %}

1 个答案:

答案 0 :(得分:8)

通常在验证为false时我使用sameas。在你的情况下:

{% if a and b is sameas(false) %}

然而documentation implies你也可以使用速记如下:

{% if a and b == false %}

请注意,此检查取决于所设置的变量。如果未设置变量,则检查true或false将失败,因为该变量的值为null

因此,如果你想检查是真还是假,并且想要确定如果没有设置值,你就会得到假;你可以使用default

{% if a and b|default(false) is sameas(false) %}

或者如果您更喜欢php风格:

{% if a and b|default(false) == false %}

这也应该有效:

{% if variant.stock.track and variant.stock.on_stock|default(false) is sameas(false) %}
      ({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}

{% if variant.stock.track and variant.stock.on_stock|default(false) == false %}
  ({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
相关问题