I’m developing a custom Shopify theme and need help with text truncation. My goal is to display product titles and descriptions in boxes with equal character counts.
Here’s what I’m trying to do:
Add up the characters in the title and description
Subtract this total from 200
Use the result to truncate the description
I’ve written some Liquid code, but it’s not working as expected:
I’ve faced a similar challenge when working on a Shopify theme. The issue you’re encountering is because Liquid’s truncate filter expects a number, not a string. Here’s a workaround I’ve used:
Instead of capturing the character limit, try using the assign tag to create a variable. Then, use the minus filter directly in the truncate operation. Something like this:
This approach should work for dynamically truncating the text based on your criteria. Remember to adjust the ‘200’ value if you want a different total character count.
Also, consider adding a fallback in case the calculation results in a negative number, which could happen with very long titles. You might use an if statement to set a minimum truncation length.
I’ve encountered this issue before, and there’s a simple solution. The problem lies in how Liquid handles variables in filters. Instead of using capture, try using assign to create your variable:
This should work as expected. However, be cautious with very short descriptions or long titles, as you might end up with negative numbers. To prevent this, you could add a safeguard: