mike
mike

Reputation: 1886

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

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

Upvotes: 78

Views: 87056

Answers (3)

Harkály Gergő
Harkály Gergő

Reputation: 831

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

Upvotes: 0

Lucas Paiano
Lucas Paiano

Reputation: 339

you could do something like this:

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

Upvotes: 19

David Jacquel
David Jacquel

Reputation: 52789

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

Upvotes: 118

Related Questions