When I try to implement custom Twig Extension in Symfony2-project, I get following error:
Unable to register extension "AppBundle\Twig\Extension\FileExtension" as it is already registered.
In my app/config/services.yml, I have following:
parameters:
app.file.twig.extension.class: AppBundle\Twig\Extension\FileExtension
services:
app.twig.file_extension:
class: '%app.file.twig.extension.class%'
tags:
- { name: 'twig.extension' }
In AppBundle/Twig/Extensions/FileExtension, i have:
<?php
namespace AppBundle\Twig\Extension;
class FileExtension extends \Twig_Extension
{
/**
* Return the functions registered as twig extensions
*
* @return array
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('file_exists', array($this, 'file_exists')),
);
}
public function getName()
{
return 'app_file';
}
}
?>
I tried to clear cache, but not working. Anybody ideas why Twig renders twice?
As @Cerad said, starting with Symfony 3.3, the twig extensions are automatically loaded and there is no need to define anything in the default
services.yml.Per the official documentation: