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

前端 未结 13 2160
太阳男子
太阳男子 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:09

    Update for Twig 2 and Twig 3.

    truncate filter is not available, instead of it you may use u-filter

    here is an example:

    {{ 'Lorem ipsum'|u.truncate(8) }}
    Lorem ip
    
    {{ 'Lorem ipsum'|u.truncate(8, '...') }}
    Lorem...
    

    Note: this filter is part of StringExtension that can be required by

    twig/string-extra
    

提交回复
热议问题