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?
In symfony 4, if you have autowire you can forget of configuring the services.yaml, your code would look the same in your services, only change the folderĀ“s path.