Skip to content

Instantly share code, notes, and snippets.

@bebraw
Created March 14, 2012 07:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bebraw/2034914 to your computer and use it in GitHub Desktop.
Save bebraw/2034914 to your computer and use it in GitHub Desktop.
Django navigation pattern
<!-- 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>
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment