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

前端 未结 13 2168
太阳男子
太阳男子 2021-01-30 00:36

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}         


        
13条回答
  •  我在风中等你
    2021-01-30 01:00

    Use the truncate filter to cut off a string after limit is reached

    {{ "Hello World!"|truncate(5) }} // default separator is ...
    

    Hello...

    You can also tell truncate to preserve whole words by setting the second parameter to true. If the last Word is on the the separator, truncate will print out the whole Word.

     {{ "Hello World!"|truncate(7, true) }} // preserve words
    

    Here Hello World!

    If you want to change the separator, just set the third parameter to your desired separator.

    {{ "Hello World!"|truncate(7, false, "??") }} 
    

    Hello W??

提交回复
热议问题