Symfony 5.1: Loading an own twig extension in a microkernel

417 views Asked by At

switching to smyfony, we struggle with some basic starting issues. We are transferring an existing project to symfony. Due to that, f.eg. configurations etc are mandatory to be in php and dynimically - so far no problem.

but: How can i load an own twig function (extension) in the microkernel ?

kernel.php

protected function configureContainer(ContainerConfigurator $container) {

  $container->extension("twig", array(
            "paths" => array(__DIR__ . "/../resources/views"),
            "auto_reload" => true,
            "cache" => false,
        ));

}

Twig is working fine and makes no problems ... undtil we need our own functions.

The (TEST !) twig function:

namespace App\Kernel\TwigExtensions;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class TwigTest extends AbstractExtension {
    public function getFunctions() {
        return array(
            new TwigFunction("test", array($this, function() { return "ABC"; })),
        );
    }
}

using the following source within "configureContainer" has no effect:

$container->services()
                ->load("App\\Kernel\\TwigExtensions\\", sprintf("%s/Kernel/TwigExtensions/TwigTest.php", __DIR__))
                ->tag('twig.extension')

Any ideas for my next step ? Thanks !

0

There are 0 answers