Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

twig

symfony

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%} 
like image 712
GRafoKI Avatar asked Oct 30 '12 16:10

GRafoKI


2 Answers

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

You need Twig 1.6

like image 140
olegkhuss Avatar answered Oct 09 '22 15:10

olegkhuss


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) }} 
like image 27
mrMantir Avatar answered Oct 09 '22 17:10

mrMantir