--- layout: default title: Operators nav: group: Liquid Variables weight: 3 --- # Operators Liquid has access to all of the logical and comparison operators. These can be used in tags such as if and unless. {% table_of_contents %} {% anchor_link "Basic Operators", "basic-operators" %}
== |
equals |
!= |
does not equal |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
or |
condition A or condition B |
and |
condition A and condition B |
contains checks for the presence of a substring inside a string.
{% highlight html %}{% raw %}
{% if product.title contains 'Pack' %}
This product's title contains the word Pack.
{% endif %}
{% endraw %}{% endhighlight %}
contains can also check for the presence of a string in an array of strings.
{% highlight html %}{% raw %}
{% if product.tags contains 'Hello' %}
This product has been tagged with 'Hello'.
{% endif %}
{% endraw %}{% endhighlight %}
You **cannot** check for the presence of an object in an array of objects using contains. This will not work:
{% highlight html %}{% raw %}
{% if product.collections contains 'Sale' %}
One of the collections this product belongs to is the Sale collection.
{% endif %}
{% endraw %}{% endhighlight %}
This will work:
{% highlight html %}{% raw %}
{% assign in_sale_collection = false %}
{% for collection in product.collections %}
{% if in_sale_collection == false and collection.title == 'Sale' %}
{% assign in_sale_collection = true %}
{% endif %}
{% endfor %}
{% if in_sale_collection %}
One of the collections this product belongs to is the Sale collection.
{% endif %}
{% endraw %}{% endhighlight %}