Twig Gravatar on Symfony 4

  gravatar, php, symfony, twig, twig-extension

I’m a newbie on Symfony and I’m having a problem to integrate the ‘ry167/twig-gravatar’ package on my project.

First, I did :

$ composer require ry167/twig-gravatar 3.0.0

And after I modified my services.yaml, which looks like this :

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

AppController:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

twig.extension.gravatar:
    class: TwigGravatar
    arguments:
        $default: ~         e.g. 'monsterid'
        $size: ~            e.g. 50
        $filterPrefix: ~    e.g. 'foo'
        $rating: ~          e.g. 'x'
        $useHttps: true
    tags:
        - { name: twig.extension }

And finally, I have this on my view:

<p>{{ '[email protected]'|grAvatar }}</p>

But I got this error:

Invalid service “twig.extension.gravatar”: class “Twig_Extension” not found while loading “TwigGravatar”

Any ideas? I can’t understand where my problem comes from…

Source: Symfony Questions

One Reply to “Twig Gravatar on Symfony 4”

  • Hi,
    I had the same problem. I ended up writing my own Twig Extension class which extends the TwigGravatar Extension Class.

    Then you can remove the service definition. The extension will automatically be loaded.

    Greets Stefanie

LEAVE A COMMENT