symfony 2 twig limit the length of the text and put three dots

134

How can I limit the length of the text, e.g., 50, and put three dots in the display?

{% if myentity.text|length > 50 %}

{% block td_text %} {{ myentity.text}}{% endblock %}

{%endif%}

This question is tagged with symfony twig

~ Asked on 2012-10-30 16:00:56

The Best Answer is


214

{{ myentity.text|length > 50 ? myentity.text|slice(0, 50) ~ '...' : myentity.text  }}

You need Twig 1.6

~ Answered on 2012-10-30 16:06:53


117

why not use twig's truncate or wordwrap filter? It belongs to twig extensions and lib is part of Symfony2.0 as i see.

{{ text|truncate(50) }}

~ Answered on 2012-10-30 16:43:40


Most Viewed Questions: