In order to split the dynamic text and the page static template moreover make a breadcrumb become easier. We can also translate the menu, dupplicate it, etc. without rewrite it many time. DRY <3
Follow the steps!
knp-menu-bundle version | |
"require": { | |
... | |
"knplabs/knp-menu-bundle": "~2.0", | |
... | |
} |
## Add knp-menu as service | |
cg_main.menu_builder: | |
class: CG\MainBundle\Menu\MenuBuilder | |
arguments: ["@knp_menu.factory"] | |
cg_main.menu.footer: | |
class: Knp\Menu\MenuItem # the service definition requires setting the class | |
factory_service: cg_main.menu_builder | |
factory_method: createFooterMenu | |
arguments: ["@request"] | |
scope: request # needed as we have the request as a dependency here | |
tags: | |
- { name: knp_menu.menu, alias: footer } # The alias is what is used to retrieve the menu |
{# A random call in your code #} | |
{% extends '::base.html.twig' %} | |
{% block body %} | |
{% block navbar %} | |
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> | |
<div class="container"> | |
<!-- Brand and toggle get grouped for better mobile display --> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="{{ path("cg_main_index") }}">CGdroid</a> | |
</div> | |
<!-- Collect the nav links, forms, and other content for toggling --> | |
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> | |
{{ knp_menu_render('footer',{'currentClass': 'active','template': 'CGMainBundle:Menu:knp_menu.html.twig'}) }} | |
</div> | |
<!-- /.navbar-collapse --> | |
</div> | |
<!-- /.container --> | |
</nav> | |
{% endblock %} | |
{% block container %} | |
{% endblock %} | |
{% endblock body %} | |