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

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

    Bugginess* in the new Drupal 8 capabilities here inspired us to write our own:

    <a href="{{ view_node }}">{% if title|length > 32 %}{% set title_array = title|split(' ') %}{% set title_word_count = 0 %}{% for ta in title_array %}{% set word_count = ta|length %}{% if title_word_count < 32 %}{% set title_word_count = title_word_count + word_count %}{{ ta }} {% endif %}{% endfor %}...{% else %}{{ title }}{% endif %}</a>
    

    This takes into consideration both words and characters (*the "word boundary" setting in D8 was displaying nothing).

    0 讨论(0)
提交回复
热议问题