Empty collections?

by pinballki - Member - 08:20AM, Oct 10, 2006

I’ve been absolutely flying for the last two days with Liquid/Vision, throughly enjoying bringing my design to life but I’ve hit a brick wall this afternoon and seem to be stuck.

I’m working on my collection.liquid template and I would like to handle empty collections in my template. Quite simply, if the collection has at least one product display x, otherwise display y.

Something like: -

{% if collection.products %}
 <p>There are products on this page.</p>
{% else %}
 <p>Sorry, there are currently no products in this section.</p>
{% endif %}

If I add an empty collection to database.yml, then Vision displays a “No Method” error – presumably because my collection template is trying to iterate over a collection (of products) that is either empty, or non-existant.

Perhaps Vision behaves differently from Shopify when it comes to empty collections? Perhaps I’m just looking at things from the wrong perspective :-)

Can anyone help me out here?

pinballki

Member

03:33PM, Oct 10, 2006

OK. Sorry. I’m stupid.

The filter size(input) does the trick: -

{{ collection.products | size }}

At least, I think it will…I can’t really test this using Vision as Liquid doesn’t like the collections I’ve setup in database.yml that have no products as members (it’s throwing “No Method” errors).

Perhaps I’m getting the syntax wrong in database.yml…

In any case I guess it’s time to upload some of my templates to Shopify and do some testing there.

The fact remains: I’m loving Shopify/Liquid/Vision. I’m actually enjoying implementing my design. I’ve never had that happen before with an ecommerce templating system.

Big thanks to Tobi and the other pixels.

tobi

Shopify

06:12PM, Oct 10, 2006

Hey Pinballki.

You should be able to use .size everywhere in liquid these days. E.g.


{% if collection.products.size  0 %}
 

Sorry, there are currently no products in this section.

{% else %}

There are products on this page.

{% endif %}

This should work as well:


{% if collection.products  empty %}
 

Sorry, there are currently no products in this section.


{% else %}

There are products on this page.


{% endif %}
---

Tobias Lütke
Shopify – Founder, CEO

pinballki

Member

10:12AM, Oct 11, 2006

Thanks, Tobi. Makes sense.

vernal

Member

03:25AM, Sep 07, 2007

Hey Tobi,

I have tried this and get the following error:

Liquid error: undefined local variable or method `name’ for #

My collection.liquid template looks like:

{% paginate collection.products by 9 %}

{% if collection.products empty %}
Sorry, there are currently no products in this section.
{% else %}

<div id="collection-description" class="textile">{{ collection.description }}</div>

{% for product in collection.products %}
<dl class="product">
<dt><a href="{{product.url}}" onclick="return Showroom.showProduct('{{product.handle}}');"><img src="{{ product.featured_image | product_img_url: 'small' }}" /></a><a href="{{product.url}}">{{product.title}}</a></dt>
<dd>{{ product.description | strip_html | truncatewords: 50 }}</dd>
<dd>{{product.price_min | money}}</dd>
<dd><a href="{{product.url}}" onclick="return Showroom.showProduct('{{product.handle}}');">View Details <img src="{{ 'arrow.gif' | asset_url }}" /></a></dd>
</dl>
{% endfor %}

{% endif %}

{% if paginate.pages > 1 %}
<div id="pagination" class="r">
{{ paginate | default_pagination }}
</div>
{% endif %}

{% endpaginate %}

Any idea what that means?

Thanks,
Vernal

tobi

Shopify

06:13PM, Sep 07, 2007

hmm this forum eats some of the equal signes ( = )

it should say: {% if collection.products.size == 0 %}

---

Tobias Lütke
Shopify – Founder, CEO

You must login to post a comment!

Don't have an account yet? Sign up for one.