Created
July 9, 2011 14:59
-
-
Save mnazim/1073637 to your computer and use it in GitHub Desktop.
Revisions
-
Mir Nazim created this gist
Jul 9, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ from django import template from django.core.urlresolvers import reverse register = template.Library() @register.simple_tag def add_active(request, name, by_path=False): """ Return the string 'active' current request.path is same as name Keyword aruguments: request -- Django request object name -- name of the url or the actual path by_path -- True if name contains a url instead of url name """ if by_path: path = name else: path = reverse(name) if request.path == path: return ' active ' return '' This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ {% load active_tag %} {% comment %} Add active class by url name {% endcomment %} <a class="{% add_active request 'url_name' %}" href="{% url url_name %}">Cars</a> {% comment %} Add active class by url path {% endcomment %} <a class="{% add_active request '/some/django/path' 1 %}" href="{% url url_name %}">Cars</a>