Active page class for selected menu items

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django import template
from django.core.urlresolvers import resolve

register = template.Library()

class ActiveUrlNode(template.Node):
    def __init__(self, request, names, return_value='active'):
        self.request = template.Variable(request)
        self.names = [template.Variable(n) for n in names]
        self.return_value = template.Variable(return_value)

    def render(self, context):
        request = self.request.resolve(context)
        any_of = False
        try:
            url = resolve(request.path_info)
            url_name = "%s:%s" % (url.namespace, url.url_name)
            for n in self.names:
                name = n.resolve(context)
                if url_name.startswith(name):
                    any_of = True
                    break
        except:
            # TODO - think a better way to log these
            print "Cannot resolve %s" % request.path_info
        return self.return_value if any_of else ''

@register.tag
def active(parser, token):
    """
        Simple tag to check which page we are on, based on resolve;
        Useful to add an 'active' css class in menu items that needs to be
        aware when they are selected.

        Usage:

            {% active request "base:index" %}
            {% active request "base:index" "base:my_view" %}
    """
    try:
        args = token.split_contents()
        return ActiveUrlNode(args[1], args[2:])
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires at least 2 arguments" % token.contents.split()[0]

More like this

  1. Active link by ronnie 2 years, 4 months ago
  2. template tag for highlighting currently active page by adunar 5 years, 6 months ago
  3. Pagination Alphabetically compatible with paginator_class by vascop 2 years ago
  4. DRY menu Custom Template Tag by sergzach 2 years, 1 month ago
  5. Template filter that divides a list into exact columns by davmuz 2 years, 3 months ago

Comments

iteratix (on December 6, 2012):

You need to add

register = template.Library()

at the top to make this snippet work; just FYI for newer folks to Django.

#

kunitoki (on January 25, 2013):

Snippet updated thanks ;)

#

kbsali (on October 28, 2013):

Nice! two important details that should be added for newbies like me : you should add "django.core.context_processors.request" to your TEMPLATE_CONTEXT_PROCESSORS in settings.py (TEMPLATE_CONTEXT_PROCESSORS might have to be added -> https://docs.djangoproject.com/en/1.5/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS when not using namespaced route names, you still need the colon : {% active request ":home" %}

#

(Forgotten your password?)