2013-04-23 8 views
0

我有一個複雜的導航,我正在用Django CMS構建。在導航中,有三個級別的頁面。在渲染2級導航時,我想先按順序顯示所有2級頁面,這些頁面是葉節點,然後顯示所有2級頁面及其子級。在Django CMS中,我怎樣才能將沒有孩子的導航節點與那些具有?

這裏的樹結構就是一個例子:

  • 首頁
  • 關於我們
    • 第二層次
    • 在深度
      • 我們是誰
      • 我們做什麼
    • Lorem存有
  • 聯繫我們
    • 諸如此類

輸出應該是這樣的:

<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> 

我寧願找到一個解決方案這不需要循環通過節點t WICE。謝謝!

回答

0

這裏是我用來顯示任意數量的每個節點的孩子們的menu.html文件:

{% 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 %} 

書中有一些Twitter的引導具體的分級,但希望這將讓你接近你需要。

+0

不幸的是,這並沒有處理在具有子節點的節點之前重新組合葉節點。 – blackrobot 2013-04-23 22:24:06

+0

啊。對不起,我忽略了那部分。我想知道在這種情況下是否可以利用「regroup」模板標籤。 – Brandon 2013-04-24 02:25:21

相關問題