In a Symfony2 project, I have created a service that implements Twig_ExtensionInterface so I can use it as a Twig filter, like {{ stuff|my_filter }}
In this service, I need to use the twig environnement so I could use twig templates for instance, so I injected it as one can do in a service :
in services.yml :
services:
meta.twig.my_extension:
class: Acme\GeneralBundle\Twig\MyExtension
tags:
- { name: twig.extension }
arguments:
twig: "@twig"
And so the service in itself looks like :
<?php
namespace Acme\GeneralBundle\Twig;
class MyExtension extends \Twig_Extension
{
public function __construct($twig)
{
$this->twig = $twig;
}
public function getFilters()
{
return array(
'my_filter' => new \Twig_Filter_Method($this, 'myFunction'),
);
}
public function myFunction($text)
{
return $this->twig->render($someTemplate,$someArguments);
}
}
And I can use it in a controller like that :
$myService = $this->container->get('Acme.twig.my_extension');
$text = $myService->myFunction($someValue);
But, of course, I get a CircularReference Error when doing so:
Circular reference detected for service "Acme.twig.my_extension",
path: "Acme.twig.my_extension -> twig".
So, what is the best way to use twig->render() function inside a custom Twig Filter ?
Thanks a lot !
$thisdoes not have such a method ...