illevens
illevens

Reputation: 373

How to truncatechars of wagtail streamfield block?

After reading all the docs, I still don't know how to truncatechars of a wagtail streamfield blocks.

{% for block in post.body %}
    {% if block.block_type == 'richtext' %}
        {{ block.value|truncatechars:100 }}
    {% endif %}
{% endfor %}

works with weird results depending on quantity of truncatechars - if it's definitely above the number of characters of all the streamfields, it displays everything (all) as expected; Now i'm putting 40 and it displays:

First rich…

third ric…

fifth …

(my text streamfields are "first richtext", "third richtext" and "fifth richtext" ; second and third blocks are pics successfully filtered out )

I think it could be fixed by adding all of the blocks into one for output, but I don't know how to do it. Do I iterate ? There's no "+" tag :/

Upvotes: 3

Views: 405

Answers (1)

illevens
illevens

Reputation: 373

{{ post.body|first|truncatechars:200 }} Is a working temporary fix, it isn't perfect because now I need to force admins to make their first streafield block a text and make it of enough length.

Upvotes: 3

Related Questions