Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquid Templating: Truncate N paragraphs

In Liquid, I'm aware you can do {% content | truncate:64 %} to truncate 64 characters and {% content | truncatewords:100 %} to truncate 100 words, but is there any way to truncate by a given number of paragraphs?

like image 299
Bolster Avatar asked Sep 06 '25 03:09

Bolster


1 Answers

I had a similar issue. Maybe you can do something like this:

{% assign truncatedContent = '' %}
{% assign paragraphs = post.content | split:'</p>' %}
{% for paragraph in paragraphs limit:N %}
    {{ truncatedContent | append: paragraph }}
    {{ truncatedContent | append: '</p>' }}
{% endfor %}

Hope it helps.

like image 67
jonathanwiesel Avatar answered Sep 08 '25 01:09

jonathanwiesel