Last active
August 29, 2015 14:22
-
-
Save Raistlfiren/afe0a82602c35f2c1bea to your computer and use it in GitHub Desktop.
Navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Navigation Template - Output Template you created as menu #} | |
{{ knp_menu_render('CoreBundle:Builder:mainMenu', | |
{ | |
'template': 'CoreBundle:partials:knp_menu.html.twig', | |
'currentClass': 'nav-active' | |
}) | |
}} | |
{# knp_menu.html.twig #} | |
{% extends 'knp_menu_base.html.twig' %} | |
{% macro attributes(attributes) %} | |
{% for name, value in attributes %} | |
{%- if value is not none and value is not sameas(false) -%} | |
{{- ' %s="%s"'|format(name, value is sameas(true) ? name|e : value|e)|raw -}} | |
{%- endif -%} | |
{%- endfor -%} | |
{% endmacro %} | |
{% block compressed_root %} | |
{% spaceless %} | |
{{ block('root') }} | |
{% endspaceless %} | |
{% endblock %} | |
{% block root %} | |
{% set listAttributes = item.childrenAttributes %} | |
{{ block('list') -}} | |
{% endblock %} | |
{% block list %} | |
{% if item.hasChildren and options.depth is not sameas(0) and item.displayChildren %} | |
{% import _self as knp_menu %} | |
<ul{{ knp_menu.attributes(listAttributes) }}> | |
{{ block('children') }} | |
</ul> | |
{% endif %} | |
{% endblock %} | |
{% block children %} | |
{# save current variables #} | |
{% set currentOptions = options %} | |
{% set currentItem = item %} | |
{# update the depth for children #} | |
{% if options.depth is not none %} | |
{% set options = options|merge({'depth': currentOptions.depth - 1}) %} | |
{% endif %} | |
{# update the matchingDepth for children #} | |
{% if options.matchingDepth is not none and options.matchingDepth > 0 %} | |
{% set options = options|merge({'matchingDepth': currentOptions.matchingDepth - 1}) %} | |
{% endif %} | |
{% for item in currentItem.children %} | |
{{ block('item') }} | |
{% endfor %} | |
{# restore current variables #} | |
{% set item = currentItem %} | |
{% set options = currentOptions %} | |
{% endblock %} | |
{% block item %} | |
{% if item.displayed %} | |
{# 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.matchingDepth) %} | |
{%- 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 %} | |
{# Mark item as "leaf" (no children) or as "branch" (has children that are displayed) #} | |
{% if item.hasChildren and options.depth is not sameas(0) %} | |
{% if options.branch_class is not empty and item.displayChildren %} | |
{%- set classes = classes|merge([options.branch_class]) %} | |
{% endif %} | |
{% elseif options.leaf_class is not empty %} | |
{%- set classes = classes|merge([options.leaf_class]) %} | |
{%- endif %} | |
{%- set attributes = item.attributes %} | |
{%- if classes is not empty %} | |
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %} | |
{%- endif %} | |
{# displaying the item #} | |
{% import _self as knp_menu %} | |
<li{{ knp_menu.attributes(attributes) }}> | |
{%- if item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %} | |
{{ block('linkElement') }} | |
{%- else %} | |
{{ block('spanElement') }} | |
{%- endif %} | |
{# render the list of children#} | |
{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %} | |
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %} | |
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %} | |
{{ block('list') }} | |
</li> | |
{% endif %} | |
{% endblock %} | |
{% block linkElement %} | |
{% import _self as knp_menu %} | |
<a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}> | |
{% if item.extras.icon is defined %} | |
<span class="navIcon {{ item.extras.icon }}" aria-hidden="true"></span> | |
{% endif %} | |
{# Check to see if item doesn't have children and the parent is not root #} | |
{% if not item.hasChildren and item.parent.name != "root" %} | |
{{ block('label') }} | |
{% else %} | |
<span class="navLabel">{{ block('label') }}</span> | |
{% endif %} | |
</a> | |
{% endblock %} | |
{% block spanElement %} | |
{% import _self as knp_menu %} | |
<span{{ knp_menu.attributes(item.labelAttributes) }}> | |
{{ block('label') }} | |
</span> | |
{% endblock %} | |
{% block label %} | |
{% if options.allow_safe_labels and item.getExtra('safe_label', false) %} | |
{{ item.label|raw }} | |
{% else %} | |
{{ item.label }} | |
{% endif %} | |
{% endblock %} | |
//Gist of MenuBuilder | |
$menu = $factory->createItem('root'); | |
$menu->addChild('Dashboard', array( | |
'route' => 'dashboard', | |
'extras' => array('icon' => 'fa fa-dashboard') | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment