I’m building a custom theme for my Shopify store and need help with text truncation. I want to create uniform text boxes by calculating the total character count from both product title and description, then subtracting from 200 to determine how much to truncate.
My calculation works fine - I can capture the difference correctly. But when I try to use this captured value with the truncate filter, it doesn’t seem to work. Can variables be used with the truncate filter?
Your problem is that {% assign variable %}...{% endassign %} captures rendered output as a string, not a number. The truncate filter can’t work with strings when it expects numeric values.
Fix it by assigning the numeric value directly without capture tags:
I hit this exact issue building a product grid with dynamic truncation based on variant counts. Using the equals sign assignment instead of the capture method keeps the numeric type intact, so truncate works properly.
This happens because Liquid handles types differently between capture blocks and direct assignment. When you use {% assign variable %}...{% endassign %}, Liquid treats everything inside as template content and renders it to a string. Since truncate needs an integer, passing a string makes it fail silently or act weird. I ran into this exact issue when building dynamic text limits for collection pages. Fix it by switching to direct assignment with the equals operator - this keeps your calculation as a number. Also, if your calculation goes negative, truncate might break. Consider adding a default filter or absolute value for cases where titles and descriptions go over your 200 character baseline.