Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple arguments in an if statement with Liquid

People also ask

How do you put multiple things in an if statement?

If you want to execute multiple statements for the else condition, enclose the code in curly brackets. If you only need to execute a single statement for the else condition, you do not need to use curly brackets.

Can an IF ELSE statement have multiple conditions?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.


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 %}

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 %}