Does Liquid have a "does not contain" or "not in array" operator?

78.9k Views Asked by At

When calling items from an array in a Liquid template, how do you call does not contain or not in array?

3

There are 3 best solutions below

2
On BEST ANSWER

unless to the rescue !

Create an [A, B, C] array.

{% assign input = "A,B,C" | split:"," %}

unless print only if constrain is not met.

This prints nothing:

{% unless input contains 'A' %}No A{% endunless %}

This prints "No Z":

{% unless input contains 'Z' %}No Z{% endunless %}
0
On

you could do something like this:

{% if collection.tags contains 'tag' %}
{% else %}
  do stuff!
{% endif %}
0
On

I just wanted to loop throw product variants in collection / search result page and display current variant format, so in product-grid-item.liquid file. In our webshop some formats have two version of colors, so I had to make to show formats only one.

I tried Lucas answer too, but it didn't work, don't understand, why.

So I solved another way: declared a string before loop and set it's value to the current variant format. Than check if next variant format is not the previously value.

{% assign currentFormat = '' %}
{%- for variant in product.variants -%}
    {%- if currentFormat != variant.title -%}
        <div>{{ variant_title }}</div>
        {% assign currentFormat = variant.title %}
    {%- endif -%}
{%- endfor -%}