$view->addExtension work, why $view->addFilter doesn't?

With Slim v3, this is my example:

$container->set(‘view’, function ($container) {
$view = new \Slim\Views\Twig(‘modules’, [
‘cache’ => false
]);
$router = $container->get(‘router’);
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($$_SERVER));
$view->addExtension(new \Slim\Views\TwigExtension($router, $uri));
$view->getEnvironment()->addFilter(new \Twig_SimpleFilter(‘json_decode’, function ($string) {
return json_decode($string);
}));
return $view;

$view->addExtension works but for ->addFilter I need $view->getEnvironment()->addFilter.

Why this?

Hi @brainstorm.

$view->addExtension works but for ->addFilter I need $view->getEnvironment()->addFilter.

Why this?

Because you’re adding the filter to the environment. This is a Twig thing, not Slim. You can read about it in the Twig docs: https://twig.symfony.com/doc/2.x/advanced.html#filters

1 Like