bebraw
3/14/2012 - 7:45 AM

Django navigation pattern

Django navigation pattern

from django.utils.translation import get_language, ugettext as _


class Navi(list):
    items = (_('Events'), _('Users'), )

    def __init__(self, cur_path):
        lang = get_language()
        first_part = '/' + cur_path.lstrip('/').split('/')[0]

        def set_status(n):
            if n['url'] == first_part:
                n['status'] == 'active'

        for i in self.items:
            o = {'name': i, 'url': '/' + slugify(i)}
            set_status(o)
            self.append(o)

# remember to attach Navi() to your template context!
# ie. 'navi': Navi(request.path)
<!-- Use {% include "includes/navigation.html" with items=navi %} --> 
<ul class="nav">
    {% for item in items %}
        <li class="{{ item.status }}">
            <a href="{{ item.url }}">{{ item.name }}</a>
        </li>
    {% endfor %}
</ul>