I’m working on custom solution for Shopify store and my problem is that I can’t manage how to avoid adding ‘ / ‘ after the last matched tag in the case
statement.
I’ve tried to use some if
statements with a forloop
filters with no result.
The last thing on my mind was finding the last matched tag in the loop that will help us avoiding ‘ / ‘ after the last element, but unfortunately I can’t manage how to do that.
Expected result: Wool/Nylon/Viscose
Here is the part of the code that compares all tags assigned to the product with the list of tags(clothing materials) required for the output.
Considering that product has such tags as Wool, Nylon and Viscose and others, non material.
Example 1
Actual & Expected Result: WoolNylonViscose
{% for tag in product.tags %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Polyamide' %}
Polyamide
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
{% endfor %}
Example 2
Filter forloop.last
was used to define the last element of the loop, but the problem is that material tags(Wool, Nylon, Viscose) can be in the middle of the product tag array. Considering product has 10 tags and material tags are spread among the array we will see next result.
Result: ///Wool/Nylon////Viscose//
{% for tag in product.tags %}
{% if forloop.last == true %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
{% else %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
/
{% endif %}
{% endfor %}
I would appreciate if you could please point on my mistakes and suggest me how can i achieve solution.
2
Answers
You can try the following:-
This should work:
More on string filters: https://help.shopify.com/themes/liquid/filters/string-filters