Supportlogo-header

10:13AM, Oct 27, 2006

wideeye Member

"Case" not working as expected

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

01:11PM, Oct 27, 2006

wideeye Member

....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!

wideeye

05:13PM, Oct 27, 2006

tobi Shopify

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 CEO // http://twitter.com/tobi

07:47PM, Oct 28, 2006

tobi Shopify

This is now in.

Tobias Lütke - Shopify CEO // http://twitter.com/tobi

11:01AM, Oct 30, 2006

wideeye Member

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)

wideeye

05:52PM, Oct 30, 2006

tobi Shopify

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 CEO // http://twitter.com/tobi

login or Sign up for an account to reply.