Answer the question
In order to leave comments, you need to log in
What is the correct way to pass data to twig in Symfony 4?
Hello.
There is some data in base.html.twig that is the same on all pages, such as menus. This menu is taken from the database. What is the correct way to pass an array of this menu's objects to base.html.twig without having to pass it all the time in every action?
Answer the question
In order to leave comments, you need to log in
Creating and using Twig templates:
symfony.com/doc/current/templating.html
As an option, we make a separate template for the menu, then we create a controller to display this menu, and in the base.html.twig main template we call this controller.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
{{ render(controller('App\\Controller\\MenuController::menu')) }}
{% block body %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question