Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic menu with symfony in template

I'm building a symfony website and I've my own code to build dynamically a menu with subitems. Right now I print it with a bit of twig code inside the base.html.twig file (master layout). The problem is when I need to pass the array with the items of the menu. I'm actually passing it to the render method of a single controller but obvsiously when I switch page I get a non-existent variable. How can I have a single location to put my php code to build the menu array and execute it every time a page is rendered?

like image 784
Stefano Avatar asked Feb 21 '23 12:02

Stefano


1 Answers

Put your menu-creating logic in a new controller, and use twig's {% render %} to include it in your page. See embedding controllers.

like image 113
Maerlyn Avatar answered Mar 04 '23 22:03

Maerlyn