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

时间:2019-06-20 09:45:20

标签: 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").

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

twig.extension.text:
     class: Twig_Extensions_Extension_Text
     tags: - { name: twig.extension }

3 个答案:

答案 0 :(得分:2)

确保已安装twig/extensions
composer require twig/extensions,如果已安装,则应该看到一个自动生成的配置文件,其中包含:

#config/packages/twig_extensions.yaml

services:
    _defaults:
        public: false
        autowire: true
        autoconfigure: true

    # Uncomment any lines below to activate that Twig extension
    #Twig\Extensions\ArrayExtension: null
    #Twig\Extensions\DateExtension: null
    #Twig\Extensions\IntlExtension: null
    #Twig\Extensions\TextExtension: null

答案 1 :(得分:0)

您的yaml文件的正确缩进可能是:

twig.extension.text:
         class: Twig_Extensions_Extension_Text
         tags:
            - { name: twig.extension }

twig.extension.text:
     class: Twig_Extensions_Extension_Text
     tags: [twig.extension]

答案 2 :(得分:0)

对于任何为了 Symfony 5 来到这里的人:

composer require twig/string-extra
composer require twig/extra-bundle

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

{{ project.title|u.truncate(30, '...') }}

截断过滤器被传递一个长度和一个可选的字符串,如果被截断则附加到末尾。