简体   繁体   中英

Liquid - switch statement with a variable

I have the following situation:

<label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: 
        {% capture color %} {{ value | handle }} {% endcapture %}

      {% assign handle = color %}
        {% case handle %}
          {% when 'red' %}
             red;
          {% when 'navy' %}
             navy;
          {% else %}
             #f00;
        {% endcase %}

background-image: url({{ value | handle | append: '.' | append: file_extension | file_url }})">

When I do {{color}} it prints the right color, but when I try to use it in a comparison statement it fails, returns blank (or rather the default which is #f00 . Same with the IF statement.

If I define the

{% assign handle = 'red' %}

then it works fine.

Have you tried using {{ value | handleize }} {{ value | handleize }} ? As far as I'm aware "handle" is not the right operator.

I had this same issue. I found this worked for me...

Try using capture instead of the assign , like this:

{% capture handle}{{ color }}{% endcapture %}

Then proceed with your case statement as before:

{% case handle %}...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM