Instantly share code, notes, and snippets.
Created
February 2, 2014 12:26
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save peshi/8767647 to your computer and use it in GitHub Desktop.
twbs list-group menu
This file contains 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
{% extends 'knp_menu.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 %} | |
{# | |
Twitter Bootstrap uses the CSS class "active" to indicate | |
the current item, so lets change that. | |
#} | |
{% set options = options|merge({'currentDepth': 0}) %} | |
{% if options.type is defined and options.currentClass is defined and options.currentClass == 'current' %} | |
{% set options = options|merge({'currentClass': 'active', 'ancestorClass': 'active'}) %} | |
{% endif %} | |
{% set listAttributes = item.childrenAttributes %} | |
{{ block('list') -}} | |
{% endblock %} | |
{% block list %} | |
{% if item.hasChildren and options.depth is not sameas(0) and item.displayChildren %} | |
{% set listClass = '' %} | |
{% if options.type is defined and options.type == 'tabs' %} | |
{% set listClass = 'nav nav-tabs' %} | |
{% elseif options.type is defined and options.type == 'pills' %} | |
{% set listClass = 'nav nav-pills' %} | |
{% elseif options.type is defined and options.type == 'stacked-tabs' %} | |
{% set listClass = 'nav nav-tabs nav-stacked' %} | |
{% elseif options.type is defined and options.type == 'stacked-pills' %} | |
{% set listClass = 'nav nav-pills nav-stacked' %} | |
{% elseif options.type is defined and options.type == 'list' %} | |
{% set listClass = 'list-group' %} | |
{% elseif options.type is defined and options.type == 'navbar' %} | |
{% set listClass = 'nav navbar-nav' %} | |
{% elseif options.type is defined and options.type == 'navbar-right' %} | |
{% set listClass = 'nav navbar-nav navbar-right' %} | |
{% endif %} | |
{% if options.pull is defined and options.pull == 'right' %} | |
{% set listClass = (listClass|default('') ~ ' pull-right')|trim %} | |
{% elseif options.pull is defined and options.pull == 'left' %} | |
{% set listClass = (listClass|default('') ~ 'pull-left')|trim %} | |
{% endif %} | |
{% set listAttributes = listAttributes|merge({'class': (listAttributes.class|default('') ~ ' ' ~ listClass)|trim}) %} | |
{# show div tag if type is list #} | |
{% if options.type is defined and options.type == 'list' %} | |
<div{{ _self.attributes(listAttributes) }}> | |
{{ block('children') }} | |
</div> | |
{% else %} | |
<ul{{ _self.attributes(listAttributes) }}> | |
{{ block('children') }} | |
</ul> | |
{% endif %} | |
{% endif %} | |
{% endblock %} | |
{% block dropdownList %} | |
{% spaceless %} | |
{% if item.hasChildren and options.depth is not sameas(0) and ((item.extras.dropdown is not defined and item.displayChildren is sameas(true) or item.extras.dropdown is defined and item.extras.dropdown is sameas(true) and item.displayChildren is sameas(true))) %} | |
{% set listAttributes = listAttributes|merge({'class': (listAttributes.class|default('') ~ ' dropdown-menu')|trim}) %} | |
<ul{{ _self.attributes(listAttributes) }}> | |
{{ block('children') }} | |
</ul> | |
{% endif %} | |
{% endspaceless %} | |
{% endblock dropdownList %} | |
{% block listList %} | |
{% spaceless %} | |
{% if item.hasChildren and options.depth is not sameas(0) and item.displayChildren %} | |
{{ block('children') }} | |
{% endif %} | |
{% endspaceless %} | |
{% endblock listList %} | |
{% block children %} | |
{# save current variables #} | |
{% set currentOptions = options %} | |
{% set currentItem = item %} | |
{# update the depth for children #} | |
{% if options.depth is not none %} | |
{% set options = currentOptions|merge({'depth': currentOptions.depth - 1}) %} | |
{% endif %} | |
{% set options = options|merge({'currentDepth': options.currentDepth + 1}) %} | |
{% 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 is defined and matcher.isCurrent(item)) or (item.current is defined and item.current) %} | |
{%- set classes = classes|merge([options.currentClass]) %} | |
{%- elseif (matcher is defined and matcher.isAncestor(item, options.depth)) or (item.currentAncestor is defined and item.currentAncestor) %} | |
{%- 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 %} | |
{%- if item.hasChildren and ((options.type is defined and options.type == 'list') or options.currentDepth is not sameas(1)) %} | |
{%- set classes = classes|merge(['dropdown-header']) %} | |
{%- elseif item.hasChildren and options.type is defined and options.type in ['tabs', 'pills', 'navbar', 'navbar-right'] and ((item.extras.dropdown is not defined and item.displayChildren is sameas(true) or item.extras.dropdown is defined and item.extras.dropdown is sameas(true) and item.displayChildren is sameas(true))) %} | |
{%- set classes = classes|merge(['dropdown']) %} | |
{%- endif %} | |
{%- if options.type is defined and options.type == 'list' %} | |
{%- set classes = classes|merge(['list-group-item']) %} | |
{% endif %} | |
{%- set attributes = item.attributes %} | |
{%- if options.type is defined and options.type == 'navbar' and attributes.divider is defined and attributes.divider is not empty and options.currentDepth is sameas(1) %} | |
{%- set classes = classes|merge(['divider-vertical']) %} | |
{%- elseif attributes.divider is defined and attributes.divider is not empty %} | |
{%- set classes = classes|merge(['divider']) %} | |
{%- endif %} | |
{%- if classes is not empty %} | |
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %} | |
{%- endif %} | |
{# display without li tag, use a only #} | |
{% if options.type is defined and options.type == 'list' %} | |
{%- if item.uri is not empty and ((matcher is defined and not matcher.isCurrent(item)) or options.currentAsLink) %} | |
{{ block('linkElement') }} | |
{%- elseif item.uri is not empty and ((item.current is defined and not item.current) or options.currentAsLink) %} | |
{{ block('linkElement') }} | |
{%- endif %} | |
{% else %} | |
{# displaying the item #} | |
<li{{ _self.attributes(attributes) }}> | |
{%- if attributes.divider is defined and attributes.divider is not empty %} | |
{%- elseif item.hasChildren and options.type is defined and options.type in ['tabs', 'pills', 'navbar', 'navbar-right'] and options.currentDepth is sameas(1) and ((item.extras.dropdown is not defined and item.displayChildren is sameas(true) or item.extras.dropdown is defined and item.extras.dropdown is sameas(true) and item.displayChildren is sameas(true))) %} | |
{{ block('dropdownElement') }} | |
{%- elseif item.hasChildren and options.type is defined and options.type in ['tabs', 'pills', 'navbar', 'navbar-right'] and options.currentDepth is sameas(1) and ((item.extras.dropdown is defined and item.extras.dropdown is sameas(false) or item.displayChildren(false) )) %} | |
{{ block('linkElement') }} | |
{%- elseif item.uri is not empty and ((matcher is defined and not matcher.isCurrent(item)) or options.currentAsLink) %} | |
{{ block('linkElement') }} | |
{%- elseif item.uri is not empty and ((item.current is defined and not item.current) 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(' ') }) %} | |
{%- if item.hasChildren and options.type is defined and (options.type == 'list' or options.currentDepth is not sameas(1)) %} | |
{{ block('listList') }} | |
{%- elseif item.hasChildren and options.type is defined and options.type in ['tabs', 'pills', 'navbar', 'navbar-right'] %} | |
{{ block('dropdownList') }} | |
{%- else %} | |
{{ block('list') }} | |
{%- endif %} | |
</li> | |
{% endif %} | |
{% endif %} | |
{% endblock %} | |
{% block linkElement %} | |
{% set attributes = item.linkAttributes %} | |
{% if options.type is defined and options.type == 'list' %} | |
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %} | |
{%- set classes = classes|merge(['list-group-item']) %} | |
{%- if (matcher is defined and matcher.isCurrent(item)) or (item.current is defined and item.current) %} | |
{%- set classes = classes|merge([options.currentClass]) %} | |
{%- elseif (matcher is defined and matcher.isAncestor(item, options.depth)) or (item.currentAncestor is defined and item.currentAncestor) %} | |
{%- 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 %} | |
{%- if classes is not empty %} | |
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %} | |
{%- endif %} | |
{% endif %} | |
{# List group with extras (badge) #} | |
<a href="{{ item.uri }}"{{ _self.attributes(attributes) }}>{{ block('badge')}}{{ block('label') }}</a> | |
{% endblock %} | |
{% block badge %} | |
{% if options.type is defined and options.type == 'list' and 'badge' in item.extras|keys %} | |
<span class="badge">{{ item.getExtra('badge') }}</span> | |
{% elseif 'badge' in item.extras|keys %} | |
<span class="badge pull-right">{{ item.getExtra('badge') }}</span> | |
{% endif %} | |
{% endblock badge %} | |
{% block dropdownElement %} | |
{% spaceless %} | |
{% set labelAttributes = item.labelAttributes %} | |
{% set labelAttributes = labelAttributes|merge({'class': (labelAttributes.class|default('') ~ ' dropdown-toggle')|trim}) %} | |
{% set labelAttributes = labelAttributes|merge({'data-toggle': 'dropdown'}) %} | |
<a href="#"{{ _self.attributes(labelAttributes) }}>{{ block('label') }} <b class="caret"></b></a> | |
{% endspaceless %} | |
{% endblock dropdownElement %} | |
{% block dividerElement %} | |
{% spaceless %} | |
{% endspaceless %} | |
{% endblock dividerElement %} | |
{% block spanElement %}<span{{ _self.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|trans }}{% endif %}{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment