In a page.[].liquid
template there are numerous ‘vendor’ products to be listed. Some with and some without a collection association.
How can I exclude a specific vendor in a for loop using ‘unless’ or a ternary?
Neither of the following generate any output within the parent container.
<div style="height: 50px;" class="ptest">
{% assign collection = product.available %}
{% for product in collection.all_vendors %}
{% if product.collection != "acme" %}
<div>yes</div>
{{ product.name }}
{% else %}
<div>no</div>
{{ product.name }}
{% endif %}
{% endfor %}
{% for product in collection.all_vendors %}
{% unless product.vendor contains "acme" %}
<div>yes</div>
{{ product.name }}
{% else %}
<div>no</div>
{{ product.name }}
{% endunless %}
{% endfor %}
</div>
2
Answers
After a lot of searching, it looks like the only solution to displaying products that A. are 'all except' a single vendor and B. are not part of any collection, is to use a more simple if-math.
Running through all products to loop those that are not part of a collection, generates the proper result.
List all vendors except “acme”:
List collection products except those with vendor “acme”:
Sorry, but your code is a mess:
{% assign collection = product.available %}
– the collection is now boolean, eithertrue
orfalse
{% for product in collection.all_vendors %}
– as of the abovecollection
doesn’t haveall_vendors
attribute.{% if product.collection != "acme" %}
– as of the above, this code will never be reached, but even if it will – theproduct
object doesn’t havecollection
attribute.{{ product.name }}
– where did you get this code?product
doesn’t havename
attribute, you have to usetitle
instead.{% for product in collection.all_vendors %}
– you go through vendors, why do you call them products?{% unless product.vendor contains "acme" %}
– as this is the next line after the above one, theproduct
would be a vendor, not a product object, so you should use it as a string i.e.{% unless product contains "acme" %}