10

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 %}
Community
  • 1
  • 1
Meules
  • 1,349
  • 4
  • 24
  • 71

1 Answers1

18

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 %}
Luceos
  • 6,629
  • 1
  • 35
  • 65
  • I'm totally confused right now! If you want to test if a == true and b == false then this doesn't work, right?! You always have none or both false/true... See my updated question – Meules Jul 29 '14 at 13:15
  • "You always have none or both false/true..." I think you need to clarify your issue, you're not making sense. – Luceos Jul 29 '14 at 13:24
  • I meant with your code you always have one false and one true. See my updated question. If a must be true and b must be false then do stuff. With your code you can have a true/false and b true/false. So it can go both ways. – Meules Jul 29 '14 at 13:32
  • I tested it on a different machine and this works... I'll accept your answer. I don't what's wrong on my side then. Enjoy your icecream ;) – Meules Jul 29 '14 at 14:03
  • 1
    Maybe your configuration `strict_variables` is set to false in one environment? – Luceos Jul 29 '14 at 14:26
  • 1
    Isn't `same as` instead of `sameas` ? – Raja Khoury Feb 22 '18 at 07:03
  • Same as used to be an alias for sameas. Seems in 2.x that might no longer be the case. – Luceos Feb 22 '18 at 12:22