我如何在 Symfony 4 中使用树枝截断

hoc*_*adi 4 twig symfony4

我想在树枝中使用 trancate 过滤器,但出现错误:

The file "D:\projets\dzairdeals\config/services.yaml" does not contain valid YAML: Indentation problem in "D:\\projets\\dzairdeals\\config/services.yaml" at line 30 (near " twig.extension.text:") in D:\projets\dzairdeals\config/services.yaml (which is loaded in resource "D:\projets\dzairdeals\config/services.yaml").
Run Code Online (Sandbox Code Playgroud)

当我尝试将此行添加到我的services.yaml

twig.extension.text:
     class: Twig_Extensions_Extension_Text
     tags: - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

She*_*rri 15

对于任何关注 Symfony 5 的人:

composer require twig/string-extra
composer require twig/extra-bundle
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用截断过滤器:

{{ project.title|u.truncate(30, '...') }}
Run Code Online (Sandbox Code Playgroud)

截断过滤器会传递一个长度和一个可选字符串,以便在截断时附加到末尾。

u.表示左侧的字符串封装在 Unicode 对象中,请参阅https://twig.symfony.com/doc/2.x/filters/u.html