Limit String Twig

前端 未结 6 1743
眼角桃花
眼角桃花 2020-12-17 18:14

How can I limit a string length? I\'m getting a description value from my database, but I want to only display a number of specific characters.

    <
6条回答
  •  庸人自扰
    2020-12-17 18:37

    Try with Truncate function:

    First, you need to activated Text extension:

    # app/config/config.yml
      services:
        twig.extension.text:
            class: Twig_Extensions_Extension_Text
            tags:
                - { name: twig.extension }
    

    Then, you can call truncate() helper within your Twig template as follow:

    {{ variable.description | truncate(100, true) }}
    

提交回复
热议问题