Created
April 29, 2013 06:13
-
-
Save benregn/5479960 to your computer and use it in GitHub Desktop.
Navbar active tab template-tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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