Skip to content

Instantly share code, notes, and snippets.

@andrewschoen
Created May 11, 2012 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewschoen/2660259 to your computer and use it in GitHub Desktop.
Save andrewschoen/2660259 to your computer and use it in GitHub Desktop.
template tag for django-cms menus that roots at a given menu level
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment