"Case" not working as expected

by wideeye - Member - 10:13AM, Oct 27, 2006

Hi

The Case statement in liquid isn’t functioning as I expected. Here’s a quick code snippet:


{% case collection.handle %}
    {% when 'menswear-jackets' %}
        {% assign: ptitle = 'menswear' %}        
    {% when 'menswear-t-shirts' %} 
        {% assign: ptitle = 'menswear' %}
    {% when 'menswear-allstars' %} 
        {% assign: ptitle = 'menswear' %}        
    {%else%}
        {% assign: ptitle = 'womenswear' %}
{% endcase %}
{{ ptitle }}

I would expect this to show “menswear” if the collection handle equalled “menswear-jackets”. Unfortunately it outputs “womenswear”. Can anyone explain how this is happening?

Also, I come from more traditional programming background, and I’m used to being able to have multiple conditions that “drop through” each other, so the above could be written:


{% case collection.handle %}
    {% when 'menswear-jackets' %}
    {% when 'menswear-t-shirts' %} 
    {% when 'menswear-allstars' %} 
        {% assign: ptitle = 'menswear' %}        
    {%else%}
        {% assign: ptitle = 'womenswear' %}
{% endcase %}
{{ ptitle }}

... is this possible with liquid??

Cheers

wideeye

Member

01:11PM, Oct 27, 2006

....or some sort of function to do something like:

if collection.handle starts with "men" 
    ptitle = "menswear" 
else if collection.handle starts with "women" 
    ptitle = "womenswear" 

It’s frustrating not being able to even access basic functions like this! I realise I’m probably trying to bend shopify into doing something it shouldn’t do, but it would be good if there was at least a temporary work around!

tobi

Shopify

05:13PM, Oct 27, 2006

The problem is that ptitle will be in scope of the case block.

I changed liquid so that assign now assigns global variables so the code you pasted should work when we deploy shopify next time ( will probably be today )

note that assign’s syntax is {% assign ptitle = 'menswear' %}

---

Tobias Lütke
Shopify – Founder, CEO

tobi

Shopify

07:47PM, Oct 28, 2006

This is now in.

---

Tobias Lütke
Shopify – Founder, CEO

wideeye

Member

11:01AM, Oct 30, 2006

Hmmm… the {% else %} catch all seems to be catching it every time!

{% case collection.handle %}
    {% when 'menswear-jackets' %}
        {% assign ptitle = 'menswear' %}        
    {% when 'menswear-t-shirts' %} 
        {% assign ptitle = 'menswear' %}
    {% else %}
        {% assign ptitle = 'womenswear' %}
{% endcase %}
{{ ptitle }}

The above code always seems to output “womenswear”. I have worked aorund it by removing the else…
{% assign ptitle = 'womenswear' %}
{% case collection.handle %}
    {% when 'menswear-jackets' %}
        {% assign ptitle = 'menswear' %}        
    {% when 'menswear-t-shirts' %} 
        {% assign ptitle = 'menswear' %}
{% endcase %}
{{ ptitle }}

...so I’m happy, but confused ;o)

tobi

Shopify

05:52PM, Oct 30, 2006

Wideeye: Thanks for the great report! Sure enough you unearthed a obscure bug in liquid which would execute else statements but discard their output if they are not needed. This of course always lead to the assign to run.

This should now be fixed

---

Tobias Lütke
Shopify – Founder, CEO

You must login to post a comment!

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