Skip to content

Instantly share code, notes, and snippets.

@UlvacMoscow
Created August 5, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UlvacMoscow/b81b7c52d7ffc419d8f56ba0b40cc794 to your computer and use it in GitHub Desktop.
Save UlvacMoscow/b81b7c52d7ffc419d8f56ba0b40cc794 to your computer and use it in GitHub Desktop.
from cms.cms_menus import Modifier
from cms.cms_menus import menu_pool
from cms.models import Page
class Level(Modifier):
post_cut = True
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
if breadcrumb:
return nodes
for node in nodes:
if not node.parent:
if post_cut:
node.menu_level = 0
else:
node.level = 0
self.mark_levels(node, post_cut)
return nodes
def mark_levels(self, node, post_cut):
for child in node.children:
if post_cut:
child.menu_level = node.menu_level + 1
else:
child.level = node.level + 1
self.mark_levels(child, post_cut)
menu_pool.register_modifier(Level)
{% load i18n menu_tags cache %}
{% for child in children %}
<li class="{% if child.ancestor %}ancestor{% endif %}
{% if child.selected %} active{% endif %}
{% if child.children %} dropdown{% endif %}">
{% if child.children %}
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
{{ child.get_menu_title }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
{{ child.level }}
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% else %}
<a href="{{ child.get_absolute_url }}" class="{{ child.level }}"><span>{{ child.get_menu_title }}</span></a>
{% endif %}
</li>
{% if class and forloop.last and not forloop.parentloop %}{% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment