Skip to content

Instantly share code, notes, and snippets.

@uranusjr
Last active December 19, 2015 03:39
Show Gist options
  • Save uranusjr/5891869 to your computer and use it in GitHub Desktop.
Save uranusjr/5891869 to your computer and use it in GitHub Desktop.
{% comment %}
templates/base.html
{% endcomment %}
{% load menu_tags %}
{% main_menu %} {# This line renders the menu #}
{% comment %}
templates/includes/main_menu.html
{% endcomment %}
{% for page in pages %}
<li{% ifequal request.path page.0 %} class="current"{% endifequal %}>
<a href="{{ page.0 }}">{{ page.1 }}</a>
</li>
{% endfor %}
'''
templatetags/menu_tags.py
'''
from django import template
from django.utils.translation import ugettext_lazy as _
register = template.Library()
MENU_PAGES = (
('/', _('Home')), # Always a good idea to support translation
('/about/', _('About')) # TODO: Use reverse() instead of hard-coded URLs
)
@register.inclusion_tag('includes/main_menu.html')
def main_menu():
return {'pages': MENU_PAGES}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment