Login

Active page class for selected menu items

Author:
kunitoki
Posted:
September 26, 2012
Language:
Python
Version:
1.4
Score:
1 (after 1 ratings)

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.

Typical usage is like:

<ul> <li class="{% active request "myapp:myview1" %}">My View 1</li> <li class="{% active request "myapp:myview2" %}">My View 2</li> </ul>

 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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" %}

#

Please login first before commenting.