Skip to content

Instantly share code, notes, and snippets.

@Gregoire-M
Forked from mrflory/Builder.php
Last active March 30, 2019 20:23
Show Gist options
  • Save Gregoire-M/4585334 to your computer and use it in GitHub Desktop.
Save Gregoire-M/4585334 to your computer and use it in GitHub Desktop.
Modified menu template to handle submenus
<?php
namespace Linkofy\CommonBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->setChildrenAttribute('class', 'nav pull-right');
$menu->addChild('User')
->setAttribute('dropdown', true);
$menu['User']->addChild('Profile', array('uri' => '#'))
->setAttribute('divider_append', true);
$menu['User']->addChild('Logout', array('uri' => '#'));
$menu->addChild('Language')
->setAttribute('dropdown', true)
->setAttribute('divider_prepend', true);
$menu['Language']->addChild('Deutsch', array('uri' => '#'));
$menu['Language']->addChild('English', array('uri' => '#'));
return $menu;
}
}
{# source: http://linkofy.wordpress.com/2012/04/02/using-knpmenubundle-and-twitter-bootstrap/ #}
{% extends 'knp_menu.html.twig' %}
{% block item %}
{% import "knp_menu.html.twig" as macros %}
{% if item.displayed %}
{%- set attributes = item.attributes %}
{%- set is_dropdown = attributes.dropdown|default(false) %}
{%- set divider_prepend = attributes.divider_prepend|default(false) %}
{%- set divider_append = attributes.divider_append|default(false) %}
{# unset bootstrap specific attributes #}
{%- set attributes = attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %}
{%- if divider_prepend %}
{{ block('dividerElement') }}
{%- endif %}
{# building the class of the item #}
{%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
{%- elseif matcher.isAncestor(item, options.depth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
{%- endif %}
{%- if item.actsLikeFirst %}
{%- set classes = classes|merge([options.firstClass]) %}
{%- endif %}
{%- if item.actsLikeLast %}
{%- set classes = classes|merge([options.lastClass]) %}
{%- endif %}
{# building the class of the children #}
{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
{# adding classes for dropdown #}
{%- if is_dropdown %}
{%- if item.level > 1 %}
{%- set classes = classes|merge(['dropdown-submenu']) %}
{%- else %}
{%- set classes = classes|merge(['dropdown']) %}
{%- endif %}
{%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %}
{%- endif %}
{# putting classes together #}
{%- if classes is not empty %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- endif %}
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
{# displaying the item #}
<li{{ macros.attributes(attributes) }}>
{%- if is_dropdown %}
{{ block('dropdownElement') }}
{%- elseif item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
{{ block('linkElement') }}
{%- else %}
{{ block('spanElement') }}
{%- endif %}
{# render the list of children#}
{{ block('list') }}
</li>
{%- if divider_append %}
{{ block('dividerElement') }}
{%- endif %}
{% endif %}
{% endblock %}
{% block dividerElement %}
{% if item.level == 1 %}
<li class="divider-vertical"></li>
{% else %}
<li class="divider"></li>
{% endif %}
{% endblock %}
{% block dropdownElement %}
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
{%- set classes = classes|merge(['dropdown-toggle']) %}
{%- set attributes = item.linkAttributes %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %}
<a href="#"{{ macros.attributes(attributes) }}>{{ block('label') }}{% if item.level <= 1 %} <b class="caret"></b>{% endif %}</a>
{% endblock %}
{% block label %}{{ item.label|trans }}{% endblock %}
{{ knp_menu_render('LinkofyCommonBundle:Builder:mainMenu', {'currentClass': 'active'}) }}
@bbeveridge
Copy link

I haven't fully brain parsed this yet but I'm wondering how to implement this while keeping my navbar inverse? Does it matter?

@joseroes
Copy link

joseroes commented Aug 7, 2013

Thank you very much!!! nice post

@cbergau
Copy link

cbergau commented Oct 26, 2013

I get this error:

Variable "matcher" does not exist in NameOfBundle:Menu:knp_menu.html.twig at line 21

@alahtarin
Copy link

Almost perfect solution! Thanks!

@jfgrissom
Copy link

Thank so much for this! Finding this was such a time saver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment