0

I have a complex navigation which I'm building with Django CMS. In the navigation, there are three levels of pages. When rendering the level 2 navigation, I'd like to first display all level 2 pages that are leaf nodes in order, and then display all level 2 pages and their children.

Here's an example of what the tree structure is:

  • Homepage
  • About Us
    • Level Two
    • In Depth
      • Who we are
      • What we do
    • Lorem Ipsum
  • Contact Us
    • Etcetera

The output should be something like this:

<ul>
  <li>Homepage</li>
  <li>About Us
    <ul class="lvl-2">
      <!-- All leaf nodes are grouped first -->
      <li>Level Two</li>
      <li>Lorem Ipsum</li>

      <!-- Then the nodes with children after -->
      <li>In Depth
        <ul class="lvl-3">
          <li>Who we are</li>
          <li>What we do</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Contact Us
    <ul class="lvl-2">
      <li>Etcetera</li>
    </ul>
  </li>
</ul>

I'd prefer to find a solution which does not require looping through the nodes twice. Thanks!

damon
  • 14,485
  • 14
  • 56
  • 75

1 Answers1

0

Here is the menu.html file that I use for displaying any number of children per node:

{% load menu_tags %}

{% for child in children %}
<li class="{% if child.selected or child.ancestor %}active{% endif %}{% if child.children %} dropdown{% endif %}">
    <a class="{% if child.children %}dropdown-toggle{% endif %}" {% if child.children %}data-toggle="dropdown"{% endif %} href="{% if child.children %}#{% else %}{{ child.attr.redirect_url|default:child.get_absolute_url }}{% endif %}">{{ child.get_menu_title|safe }}{% if child.children %} <b class="caret"></b>{% endif %}</a>
    {% if child.children %}
    <ul class="dropdown-menu">
        {% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
    </ul>
    {% endif %}
</li>

{% endfor %}

There is some Twitter Bootstrap specific classing in there, but hopefully this will get you close to what you need.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144