Skip to content

Instantly share code, notes, and snippets.

@benregn
Created April 29, 2013 06:13
Show Gist options
  • Select an option

  • Save benregn/5479960 to your computer and use it in GitHub Desktop.

Select an option

Save benregn/5479960 to your computer and use it in GitHub Desktop.
Navbar active tab template-tag
# http://stackoverflow.com/a/656328/170172
from django import template
register = template.Library()
@register.tag
def active(parser, token):
args = token.split_contents()
template_tag = args[0]
if len(args) < 2:
raise (
template.TemplateSyntaxError,
"%r tag requires at least one argument" % template_tag
)
return NavSelectedNode(args[1])
class NavSelectedNode(template.Node):
def __init__(self, url):
self.url = url
def render(self, context):
path = context['request'].path
pValue = template.Variable(self.url).resolve(context)
if path == pValue:
return 'active'
return ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment