调用时找不到自定义 Twig 过滤器

Custom Twig filter not found when called

我正在尝试创建自己的 Twig 过滤器。我遵循了这个教程 Symfony Official Book。 但是我得到这个错误 The filter "avatar" does not exist in src/Acme/Bundle/StoryBundle/Resources/views/Story/storyList.html.twig

这是我的 AvatarExtension.php

<?php

namespace AppBundle\Twig;

class AvatarExtension extends \Twig_Extension
{
    public function getFilters()
    {

        return array(
            new \Twig_SimpleFilter('avatar', array($this, 'avatar')),
        );

    }

    public function getName()
    {
        return 'avatar_extension';
    }

    public function avatar($user)
    {
        if ($user->getPicture() && $user->getPicture() != '') {
            return $user->getPicture();
        } else {
            return '/images/default-avatar.jpg';
        }
    }
}

还有我的AppBundle/Resources/config/services.yml

services:
    app.twig.avatar_extension:
        class: AppBundle\Twig\AvatarExtension
        tags:
        – { name: twig.extension }

使用过滤器的模板与 Twig 扩展不在同一个包中,但由于它是一项服务,所以应该没有问题。 我是这样称呼它的:{{ story.author|avatar }}

您知道问题出在哪里吗?

编辑

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
      uploadTmpDir: %upload.tmp.relative.dir%

好的,我找到了解决方案。这是 services.yml

app.twig.avatar_extension:
        class: AppBundle\Twig\AvatarExtension
        tags:
            - { name: twig.extension }

这是扩展类:

<?php

namespace AppBundle\Twig;

class AvatarExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('avatar', array($this, 'avatarFilter')),
        );
    }

    public function avatarFilter($user)
    {
        if ($user->getPicture() && $user->getPicture() != '') {
            return $user->getPicture();
        } else {
            return '/images/default-avatar.jpg';
        }
    }

    public function getName()
    {
        return 'avatar_extension';
    }
}

我猜函数名一定要有Filter后缀