user3342506
user3342506

Reputation: 159

How to send data to base template without controller?

I need to get the data from entity in the base template (twig), but not rendering this data from controller.

Specifically, I want to realize the menu. Menu labels stored in the database (Page entity). I have many controllers and I don't want to repeat the code of the entity handling in each controller.

I could extend the controller's classes, but I want to avoid stuff like this:

    return $this->render('... .html.twig', 
                          array(
                          ...
                          'menu' => $labels,
                          ...
                           )
                        );

in each of the controllers.

Upvotes: 1

Views: 2329

Answers (1)

Maerlyn
Maerlyn

Reputation: 34107

This is the perfect use-case for an embedded controller. You can call a new controller from your template for rendering a part of your response, in this case the menu. It can have any logic a controller can, meaning you can query your database, build your menu structure and render a twig file for outputting it as html.

Upvotes: 3

Related Questions