Skip to content

Instantly share code, notes, and snippets.

@mnazim
Created July 9, 2011 14:59
Show Gist options
  • Select an option

  • Save mnazim/1073637 to your computer and use it in GitHub Desktop.

Select an option

Save mnazim/1073637 to your computer and use it in GitHub Desktop.

Revisions

  1. Mir Nazim created this gist Jul 9, 2011.
    23 changes: 23 additions & 0 deletions active_tag.py
    Original 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 ''
    6 changes: 6 additions & 0 deletions gistfile1.mustache
    Original 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>