I have built a twig extension to do some things and one of them is render a template. How can I access from inside the twig extension the engine environment and call the Render
You can define the extension so that it needs the environment. Twig will automatically pass it to the function.
use Twig\Environment;
use Twig\TwigFunction;
public function getFunctions()
{
return [
new TwigFunction(
'myfunction',
[$this, 'myFunction'],
['needs_environment' => true]
),
];
}
public function myFunction(Environment $environment, string $someParam)
{
// ...
}
public function getFunctions()
{
return array(
new \Twig_SimpleFunction(
'myfunction',
array($this, 'myFunction'),
array('needs_environment' => true)
),
);
}
public function myFunction(\Twig_Environment $environment, string $someParam)
{
// ...
}