question is there – How to get results from the service in a Twig template?
Versions:
Symfony 6.1.12
PHP 8.1.6
I have the class Navigation, that returns Navigation array with the method getNavigation():
namespace AppService;
use SymfonyComponentRoutingRouterInterface;
class Navigation
{
private RouterInterface $router;
private array $navigation = [];
public function __construct(RouterInterface $router)
{
$this->router = $router;
$this->setNavigation();
}
public function setNavigation(array $nav = []): void
{
$this->navigation = $nav;
if(empty($this->navigation)) {
$this->navigation = [
'Home' => $this->router->generate('homepage'),
'Products' => $this->router->generate('products'),
'Contacts' => $this->router->generate('contacts'),
];
}
}
public function getNavigation(): Array
{
return $this->navigation;
}
}
Now it works by calling the class in each Controller (for example:)
public function __construct(Navigation $navigation)
{
$this->navigation = $navigation;
}
// #[Route('/', name: 'homepage')]
public function index(): Response
{
return $this->render('home/index.html.twig', [
'navigation' => $this->navigation->getNavigation(),
]);
}
Is it possible to get access from this service in a nav.html.twig template
without calling this instance everytime?
I’ve tried to save the service in services.yaml by
services:
app.navigation:
class: AppServiceNavigation
and then get the access by {{ app.navigation.getNavigation() }}
in twig, but it does’nt work.
Thanks in advance for answers.
2
Answers
There are 2 ways.
Symfony Twig Globals
And use that in twig:
Symfony lazy twig extension
And I think you should change service define method. Take a look Symfony services
in my opinion the best way is to write a twig extension and call the service class
https://symfony.com/doc/current/templates.html#writing-a-twig-extension
in a twig template