I’m working on a new Shopify template for my store. I’m trying to add up the number of chars in my title, and the number of chars in my description. Subtract this total from 200, and use the result in my truncate.
(it’s because I want to get the same number of chars in each box)
I thought the below code would work…. The capture bit works (nb: if my total chars = 204, the result is 4) but it seems that truncate can’t work off a variable?
{% capture truncateBy %}
{{ product.title.size | plus: product.description.size | minus: 200 }}
{% endcapture %}
<p>{{ product.description | strip_html | replace: ' ', ' ' | truncate: truncateBy }}</p>
Your code seems to work for me. In what way does it not work? Does it show an error, the whole description, or “…”?
An alternative method you could try is to use actual variable assignment rather than capture:
{% assign truncateBy = product.title.size | plus: product.description.size | minus: 200 %}Edit: Truncate will truncate to the given number of characters, so if you only want 200 characters, then you can just use that constant directory as an argument to truncate:
<p>{{ product.description | strip_html | replace: ' ', ' ' | truncate: 200 }}</p>See the documentation of the truncate filter for details.