Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twig if one is true and other is false

Tags:

twig

In relation to this question.

What do you do when a == true and b == false? This must be Before down voting believe it or not but there's nothing to find on this.

So:

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

You should say that this should work but that isn't:

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

UPDATE2 This works because one is true and two is false

{% if variant.stock.track == true %} 
{% if variant.stock.on_stock == false %}
  ({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
{% endif %}
like image 233
Meules Avatar asked Jul 29 '14 12:07

Meules


1 Answers

Normally when verifying for false I use sameas. In your case:

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

However documentation implies you can also use shorthand if's like this:

{% if a and b == false %}

Please note that this check depends on the variable being set. If no variable is set, checking for true or false will fail because the variable will have the value null.

So if you want to check for true or false and want to be sure that if no value is set you get false; you might use default:

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

or if you prefer the php-style:

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

This should work as well:

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

or

{% if variant.stock.track and variant.stock.on_stock|default(false) == false %}
  ({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
like image 116
Luceos Avatar answered Oct 04 '22 04:10

Luceos