F
F
fman22018-02-10 14:58:12
symfony
fman2, 2018-02-10 14:58:12

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

2 answer(s)
M
Maxim Fedorov, 2018-02-10
@fman2

Creating and using Twig templates:
symfony.com/doc/current/templating.html

V
virtex2000, 2019-05-14
@virtex2000

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 question

Ask a Question

731 491 924 answers to any question