if statement curiosity

Shopify Expert
143 0 0
Reading the Shopify wiki I'd assume that if I used the following code:


	{% if tags %}
   	
    {% for tag in collection.tags %}
  • {{ tag | highlight_active_tag | link_to_tag: tag }}
  • {% endfor %}
{% endif %}
If I'm on a collection page where products have tags I'd get a list but if the products in a collection have no tags, nothing should appear, right? Except where I have a collection with no products yet added to it, I get this;

	
This is not there on other pages such as the index template, just on the collections template. (code is in my theme template) Its not a major issue though it does invalidate the page.
Winners - 2008 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ Finalists - 2007 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ HQ: http://www.propellerglobal.com/ | Domains & Hosting: http://domains.propellerglobal.com/ Shopify Stores: http://shop.proplaster.com.au | http://www.thekhalsaraj.com | http://www.productivegardens.com.au | http://www.elephantrose.com.au | http://www.stradbrokeweddings.com.au | http://www.solution-central.com.au
0 Likes
Tourist
338 0 1

Haven’t tried this but could you use

{% if collection.products.size >= 1 && tags %}

or wrap your tags ‘if’ in

{% if collection.products.size >= 1 %}
Mike www.anothervision.co.uk
0 Likes
Excursionist
727 0 7

@damien: that doesn’t make sense. are you sure that that ul isn’t from some other code you’ve got on the collection template, eg. an unsatisfied conditional with the “ul” outside the loop? – as opposed to the theme template?

i would think it would be from this:

ul
for products in collection
li-product
endfor
ul

operation absurdist feature requests: terminated (http://en.wikipedia.org/wiki/Absurdism)
0 Likes
New Member
251 0 0

Hmm.. you’re looking up
tags

but then working with
collection.tags

0 Likes
Shopify Expert
143 0 0
Hmm.. you’re looking up
tags

but then working with
collection.tags

I tried

{% if collection.tags %}

Doesnt make any difference – I’ve still got the empty ul’s

@Christina – the code is nested (I simplified it as I’ve tried it wouthout the other code and got the same results). If the ul’s are inside the ‘if’ statement, they shouldnt display where there are no tags, right?

- here is the full code (if.tags changed to if collection.tags as per daniel’s suggestion – which i think I’ve interpreted right?).

Its definitely in the theme.liquid


<ul id="collections">
    {% if collection.tags %}
    <li>{{ collection.title | link_to: link.url }}
        <ul>
            {% for tag in collection.tags %}  
            <li>{{ tag | highlight_active_tag | link_to_tag: tag }}</li>
            {% endfor %}
        </ul>
    </li>
    {% endif %}
    {% for link in linklists.collections.links %}
    <li>{{ link.title | link_to: link.url }}</li>
    {% endfor %}
</ul>

@lorem-ipsum.



{% if collection.products.size >= 1 && tags %}

and 

{% if collection.products.size >= 1 %}

This works fine where a collection has no products but where we have products present but with no tags – (ie in this instance not in a sub-category), we’re back with the original empty ul’s.

It seems that on collection pages, the ‘if’ statement is being ignored, however the ‘for’ loop obviously is (correctly) not returning anything.

Scratching my head here????

I think we could use some Tobi input here please.

Winners - 2008 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ Finalists - 2007 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ HQ: http://www.propellerglobal.com/ | Domains & Hosting: http://domains.propellerglobal.com/ Shopify Stores: http://shop.proplaster.com.au | http://www.thekhalsaraj.com | http://www.productivegardens.com.au | http://www.elephantrose.com.au | http://www.stradbrokeweddings.com.au | http://www.solution-central.com.au
0 Likes
New Member
251 0 0

Try this


{% if tags.size > 0 %}
...
{% endif %}
0 Likes
Shopify Expert
143 0 0

Yep, that works, thanks Daniel.

Winners - 2008 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ Finalists - 2007 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ HQ: http://www.propellerglobal.com/ | Domains & Hosting: http://domains.propellerglobal.com/ Shopify Stores: http://shop.proplaster.com.au | http://www.thekhalsaraj.com | http://www.productivegardens.com.au | http://www.elephantrose.com.au | http://www.stradbrokeweddings.com.au | http://www.solution-central.com.au
0 Likes
New Member
63 0 0

I know this thread is a year old… but many thanks Daniel. I was having the same problem… but worse because I had a

<h3>Tags</h3>
header inside my if statement… so I’d end up with a nice pretty styled h3 tag with a nice 4 px bottom border… etc… and nothing under it.

http://uniondesign.ca :: design+code
0 Likes
Shopify Expert
143 0 0

Knowledge and my memory are perishable - thankfully this is still here to stop me banging my head against the wall, again…

Winners - 2008 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ Finalists - 2007 McFarlane Prize for Excellence in Australian Web Design: http://mcfarlaneprize.com/ HQ: http://www.propellerglobal.com/ | Domains & Hosting: http://domains.propellerglobal.com/ Shopify Stores: http://shop.proplaster.com.au | http://www.thekhalsaraj.com | http://www.productivegardens.com.au | http://www.elephantrose.com.au | http://www.stradbrokeweddings.com.au | http://www.solution-central.com.au
0 Likes
Highlighted
Shopify Partner
12 0 2

in order to get this to work, i had to use

 

{% if collection.tags.size > 0 %}

0 Likes