38

I want to use an if statement in Liquid with multiple conditionals. Something like:

{% if (include.featured == "true" and product.featured == "true") or (include.featured == "false" and product.featured == "false") %}

Multiple conditionals don't seem to work. Have I got the syntax wrong or can Liquid not handle this sort of if statement?

Fisu
  • 3,294
  • 9
  • 39
  • 61

2 Answers2

47

Unfortunately, Liquid has a poor implementation of boolean algebra.

Using Liquid's operators and tags, here is a dirty way to achieve it:

{% if include.featured == true and product.featured == true %}
      {% assign test = true %}
{% endif %}

{% if include.featured == false and product.featured == false %}
      {% assign test = true %}
{% endif %}

{% if test %}
      Yepeeee!
{% endif %}
11

Another way you can condense this is to combine else if statements, and booleans don't necessarily need the "==" when evaluating true:

{% if include.featured and product.featured %}
      {% assign test = true %}
{% elsif include.featured == false and product.featured == false %}
      {% assign test = false %}
{% endif %}
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
Daniel
  • 111
  • 1
  • 2