public
Created

template tag for django-cms menus that roots at a given menu level

  • Download Gist
custom_nav_tags.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
from menus.menu_pool import menu_pool
from django import template
from menus.templatetags.menu_tags import cut_after
 
register = template.Library()
 
@register.inclusion_tag('cms/dummy.html', takes_context=True)
def show_rooted_sub_menu(context, levels=100, root_level=0, template="menu/sub_menu.html"):
"""
show the sub menu of the current nav-node.
-levels: how many levels deep
-temlplate: template used to render the navigation
"""
 
try:
# If there's an exception (500), default context_processors may not be called.
request = context['request']
except KeyError:
return {'template': 'menu/empty.html'}
nodes = menu_pool.get_nodes(request)
children = []
for node in nodes:
if (node.ancestor and node.level == root_level) or (node.selected and node.level == root_level):
cut_after(node, levels, [])
children = node.children
for child in children:
child.parent = None
children = menu_pool.apply_modifiers(children, request, post_cut=True)
context.update({'children':children,
'template':template,
'from_level':0,
'to_level':0,
'extra_inactive':0,
'extra_active':0
})
return context

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.