Ad

Shopify: Remove Whitespace Foorloop

- 1 answer

I have a simple foorloop to output how many variants are in each line item; however the | rstrip does not seem to work at all!

Here is my for loop

{% for line_item in order.line_items %}
     {{line_item.variant_id | rstrip}} 
     {%- unless forloop.last == true  -%},{%- endunless -%}          
{% endfor %}

But still that outputs variants with an ending whitespace of each variants as in this example:

11111111111(whitespace)22222222222(whitespace)33333333333(whitespace)

Or as in

11111111111 22222222222 33333333333

Ad

Answer

In liquid, you can in fact do without filters. If you just add both {%- -%}and{- -} all white spaces between values will be removed. Given the example above, the whitespace issue has been solved by replacing it with the following:

{%- for line_item in order.line_items -%}
     {{-line_item.variant_id-}} 
     {%- unless forloop.last == true  -%},{%- endunless -%}          
{%- endfor -%}
Ad
source: stackoverflow.com
Ad