Shopify themes, liquid, logos, and UX
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: ' ' | remove: '\n' | strip_html | remove: '[product]' | strip -%}
<!-- Handle [{{ handle_product }}] -->
</div>
{% 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 ] -->
</div>
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!
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:
https://shopify.dev/api/liquid/filters/string-filters#handle-handleize
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 goes...it would be nice to know if the code is only misbehaving for me.
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!
User | RANK |
---|---|
212 | |
150 | |
69 | |
46 | |
40 |
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023