Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - populate navigation bar in layout.html.twig on every page request

Tags:

symfony

In my layout.html.twig I have a navigation bar with some links. I want the links to be populated from a database.

For example, in the navigation bar I have a drop down of 'Recent Products'. How can I populate the dropdown in the main layout.html.twig from a database? (of course it'll be cached in the future)

Do I need to create a filter?

Any help would be great. Thanks

Note: In Symfony 1.4 I would have used a component. Does this exist in Symfony 2?

like image 723
Flukey Avatar asked Aug 22 '11 13:08

Flukey


2 Answers

The answer is here: http://symfony.com/doc/2.0/book/templating.html#embedding-controllers

I needed to create a sub-controller and then embed the sub-controller view into the main twig layout.

like image 60
Flukey Avatar answered Sep 30 '22 07:09

Flukey


Did you had a look @ the knplabs menu-bundle? https://github.com/knplabs/KnpMenuBundle

they are doing exactly what you want to… have look in the source to see how they achieve it.

like image 29
Flask Avatar answered Sep 30 '22 07:09

Flask