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

GRafoKI picture GRafoKI · Oct 30, 2012 · Viewed 118.8k times · Source

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%}

Answer

olegkhuss picture olegkhuss · Oct 30, 2012
{{ myentity.text|length > 50 ? myentity.text|slice(0, 50) ~ '...' : myentity.text  }}

You need Twig 1.6