Created
November 10, 2012 23:08
-
-
Save ionelmc/4052893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template import Library | |
register = Library() | |
@register.simple_tag(takes_context=True) | |
def active_page(context, page_name): | |
active = active_class(context, page_name) | |
return ' class="%s" ' % active if active else active | |
@register.simple_tag(takes_context=True) | |
def active_class(context, page_name): | |
if context.get("current_page") == page_name: | |
return 'active' | |
else: | |
return '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
<li {% active_page 'register' %}><a href="{% url 'register' %}">Register</a></li> | |
... | |
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls.defaults import patterns, url | |
urlpatterns = patterns('app.views', | |
url(r'^register/$', 'register', { | |
'template_name': 'appregister.html', | |
'extra_context': {'current_page': 'register'}, | |
}, name='register'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
def register(request, template_name=None, extra_context={}): | |
form = ... | |
return render(request, template_name, dict( | |
form=form, | |
**extra_context | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment