tags:

views:

30

answers:

1

Hi there,

I'm using django-treemenus

I'm curious to see if there is a better way to write out my menu.html which is my menu template. For each level that I add to my menu I have to manually add a level to my menu template.

Here is my menu.html (menu template). It works, but could it be written more efficiently?

{% load tree_menu_tags %}
{% ifequal menu_type "horizontal" %}
    <ul><!-- Root -->
        {% for menu_item in menu.root_item.children %}
                <!-- Level 1-->
                {% if menu_item.has_children %}                    
                    <li><a href="{{ menu_item.url }}">{{ menu_item.caption }}</a>
                        <ul>
                            {% for child in menu_item.children %}
                                <!-- Level 2-->
                                {% if child.has_children %}
                                    <li><a href="{{ child.url }}">{{ child.caption }}</a>
                                        <ul>
                                            {% for childchild in child.children %}
                                                <!-- Level 3-->
                                                {% if childchild.has_children %}
                                                    <li><a href="{{ childchild.url }}">{{ childchild.caption }}</a>
                                                        <ul>
                                                            {% for childchildchild in childchild.children %}                                                                    
                                                                {% show_menu_item childchildchild %}
                                                            {% endfor %}
                                                        </ul>
                                                    </li>
                                                {% else %}
                                                    <li><a href="{{ childchild.url }}">{{ childchild.caption }}</a></li>
                                                {% endif %}
                                                <!-- End Level 3 -->
                                            {% endfor %}
                                        </ul>
                                    </li>
                                {% else %}
                                    <li><a href="{{ child.url }}">{{ child.caption }}</a></li>
                                {% endif %}
                                <!-- End Level 2 -->
                            {% endfor %}
                        </ul>
                    </li>
                {% else %}
                    <li><a href="{{ menu_item.url }}">{{ menu_item.caption }}</a></li>
                {% endif %}    
                <!-- End Level 1 -->
        {% endfor %}
    </ul><!-- End Root -->
{% endifequal %}
+1  A: 

I'd use a custom template tag for this kind of stuff: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Here you can find some great examples: http://code.google.com/p/django-page-cms/source/browse/trunk/pages/templatetags/pages_tags.py?r=783

bx2