Skip to content

Instantly share code, notes, and snippets.

@romainnorberg
Created October 19, 2020 11:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainnorberg/a68e5bfa229d974f9b79027be3cbd058 to your computer and use it in GitHub Desktop.
Save romainnorberg/a68e5bfa229d974f9b79027be3cbd058 to your computer and use it in GitHub Desktop.
Symfony Twig Closure Extension
<?php
declare(strict_types=1);
namespace AppBundle\Extension;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ClosureExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('closure', [$this, 'executeClosure'])
];
}
public function executeClosure(\Closure $closure, $arguments)
{
return $closure(...$arguments);
}
public function getName(): string
{
return 'closure_twig_extension';
}
}
AppBundle\Extension\ClosureExtension:
public: false
tags:
- { name: twig.extension }
closure(<closure>, [<parameters>])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment