Symfony 4.1 twig extension

不想你离开。 提交于 2020-06-26 12:25:23

问题


In Symfony 4.1 I created an twig extension and I tried to use it as an service

twig.extension.active.algos:
    class: App\Twig\AppExtension
    public: true
    tags:
        - { name: twig.extension, priority: 1024 }

Unfortunately I receive 'Unable to register extension "App\Twig\AppExtension" as it is already registered' After many searches I saw that there was a bag in the version of symfony 3.4 but they say the error would have solved. So it's my mistake or just another mistake from symfony team.

My extension is:

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends \Twig_Extension {

public function getFunctions() {
    return array(
        new \Twig_SimpleFunction('get_active_algos', array($this, getActiveAlgos')),
    );
}

public function getActiveAlgos()
{
    return [1,2,3];
}

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

回答1:


Got bored. Here is a working example of a custom twig function for S4.1. No service configuration required (Update: except for the added $answer argument). I even injected the default entity manager using autowire just because.

namespace App\Twig;

use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class TwigExtension extends AbstractExtension
{
    private $em;
    private $answer;

    public function __construct(EntityManagerInterface $em, int $answer)
    {
        $this->em = $em;
        $this->answer = $answer;
    }
    public function getFunctions()
    {
        return array(
            new TwigFunction('get_active_algos', [$this, 'getActiveAlgos']),
        );
    }
    public function getActiveAlgos()
    {
        $dbName = $this->em->getConnection()->getDatabase();
        return 'Some Active Algos ' . $dbName . ' ' . $answer;
    }
}

Update: Based on the first comment, I updated the example to show injecting a scaler parameter which autowire cannot handle.

# services.yaml
App\Twig\TwigExtension:
    $answer: 42

Note that there is still no need to tag the service as an extension. Autoconfig takes care of that by automatically tagging all classes which extend the AbstractExtension.



来源:https://stackoverflow.com/questions/51405508/symfony-4-1-twig-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!