Comparisons in Liquid

How to compare

When comparing variables always make sure you are using the correct data types. If the operand types are mismatched the system tries to make the operation work by converting one of the operands like this:

  • comparing string with numeric - the system tries to convert the string operand to numeric. If this is not possible both operands will be treated as string. For example:
    • 5 > "7" - "7" will be converted to numeric and the operation will be false
    • 5 > "a132" - "a123" cannot be compared to numeric, this will be treated as "5" > "a123"
  • comparing string with dateTime -the system will try to convert the string operand to a dateTime variable, if this fails the operands will be compared as string. For example:
    • 2015-01-13T10:47:54 > "2012-01-13T10:47:54" - the second operand will be converted to dateTime and the operation will be true
    • 2015-01-13T10:47:54 > "Jul 2012" - the second operand cannot be compared to dateTime and the comparison will be treated as a string to string comparison.

To convert variables take a look at the convert filter in the Supported filters article.

Number variables

Comparing numeric values is pretty straight forward. You can use any of the operators detailed in the - Supported operators article, for example:

Do note the decimal sepparator is a single dot.

  {% assign var1 = 5.123 %}
  {% assign var2 = 2 %}

  {% if var1 > var2 %}
    <p>var1 is larger</p>
  {% else %}
    <p>var2 is larger</p>
  {% endif %}

You can also use the OR and AND operators to create more complex comparisons:

  {% if userData.age > 18 and userData.city == "Chicago" %}
    <a href="/chicago-offers.html">See our special offers for Chicago.</p>
  {% endif %}

String variables

When comparing string type variables the characters are compared 1-1, for example

  {% assign var1 = "John" %}
  {% assign var2 = "S" %}

  {% if var1 > var2 %}
    <p>var1</p>
  {% else %}
    <p>var2</p>
  {% endif %}

The comparison above will output var2 because "S" is considered larger than "J".

  {% assign var1 = "J1237c" %}
  {% assign var2 = "J1237a" %}

  {% if var1 > var2 %}
    <p>var1</p>
  {% else %}
    <p>var2</p>
  {% endif %}

This time the comparison will output var1 because the first 5 characters are identical and at the 6-th character "c" is considered larger than "a".

  {% assign var1 = "524a1" %}
  {% assign var2 = 3 %}

  {% if var1 > var2 %}
    <p>var1</p>
  {% else %}
    <p>var2</p>
  {% endif %}

In this example we are comparing a string variable with a numeric one. The string variable however cannot be converted to a string so the comparison will be between two string variables - "524a1" and "3". In this case 524a1 is considered larger.

Date variables

Comparing date type variables is straight forward. Provided at least one of the operands is properly formatted as dateTime and the other operand is either dateTime or string any of the operators detailed in the Supported operators article can be used.