Login

Simple templatetag to mark navigation as active using view names or regex

Author:
gluchet
Posted:
May 23, 2013
Language:
Python
Version:
1.5
Tags:
regex navigation active
Score:
0 (after 0 ratings)

Theses two templatetags make easy to add an active class on a navigation link. The first one is based on a regexp an search if the request path match it. The second one simply use view names.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.template import Library
from django.core.urlresolvers import resolve
import re

register = Library()

@register.simple_tag
def renavactive(request, pattern):
    """
    {% renavactive request "^/a_regex" %}
    """
    if re.search(pattern, request.path):
        return "active"
    return ""

@register.simple_tag
def navactive(request, urls):
    """
    {% navactive request "view_name another_view_name" %}
    """
    url_name = resolve(request.path).url_name
    if url_name in urls.split():
        return "active"
    return ""

More like this

  1. Active link by ronnie 3 years, 3 months ago
  2. Active class for navigation link by cschand 5 years, 5 months ago
  3. Using URLs for highlighting sections in menu by diverman 4 years, 6 months ago
  4. AdminPeepingMiddleware by buriy 7 years, 5 months ago
  5. Active page class for selected menu items by kunitoki 2 years, 5 months ago

Comments

Please login first before commenting.