New Shopify Certification now available: Liquid Storefronts for Theme Developers

Unable to strip trailing whitespace...

New Member
13 0 0

Hey y'all!


I'm working on the boss's shop and have run into some funky behavior. My goal is to strip the whitespace from a string so the code can build a product block from the resulting handle, giving me, in this example, "gem-chip-jars".


Here's the relevant Liquid/HTML code:



{% assign my_description = article.content | split: '<p>' %}  
{% assign my_description_size = my_description.size %}
{% for i in (1..my_description_size) %}
  {% if my_description[i] contains '[product]' %}
    <div class="show-product-list">
      <!-- Raw [{{ my_description[i] }}] -->
    {%- assign handle_product = my_description[i]| remove: ' ' | remove: '&nbsp;' | remove: '\n' | strip_html | remove: '[product]' | strip -%}
      <!-- Handle [{{ handle_product }}] -->
  {% else %}
    <p>{{ my_description[i] }}
  {% endif %}
{%- endfor -%}




However, strip, rstrip, and {%- xxx -%} are not removing the whitespace at the end of the string, which gives me this result (white space and line break included):



            <div class="show-product-list">
              <!-- Raw [[product]gem-chip-jars </p>
] --><!-- Handle [gem-chip-jars ] -->



There is no product with the handle "gem-chip-jars " so the code is not executed. I've tried every combination of strip, rstrip, replace, and remove that I can think of, and even splitting up the string filters step-by-step, but I still can't get rid of that trailing space.


I realize that I can just, y'know, NOT put a trailing space at the end, but at this point it's the principle of the thing and I must beat it into submission for my own peace of mind (and just in case someone forgets to make sure that they haven't inadvertently added extra spaces to the string).


Please tell me it's something silly so I can get on with my life. TYIA!

Replies 3 (3)
Shopify Expert
11159 222 2274

Interesting. I would have expected strip to work there. I'd have to check this in a test shop to see if I can replicate it.

In the meantime, the handleize filter is quite useful:


★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. ★
New Member
13 0 0

Thanks, @Jason!! The handleize filter does indeed do the trick after the rest of the flaming hoops failed to.


Edit: Apparently my page refresh wasn't quite refreshing enough. Turns out handleize doesn't work either. I get the same result as all the other filters I've tried. I'm stumped.


I'm interested to hear how your testing would be nice to know if the code is only misbehaving for me.

New Member
13 0 0

Hi, @Jason - not sure if you saw my earlier reply after my edit or before, but I wanted to touch base in case you've had a chance to do any testing of your own. Handleize also failed for me (I was a bit quick on the trigger to initially post that it worked).


No worries if you haven't looked at it yet. It's not an urgent issue!