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


Custom Twig filter not found when called

我正在尝试创建自己的Twig过滤器。我看了这本芭蕾舞团的官方书籍。但是我得到了这个错误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%

好的,我找到了解决方案。这是服务。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';
    }
}

我想函数名称必须有过滤器后缀