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 method?
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)
{
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With