Created
October 12, 2013 17:02
-
-
Save smant/6952357 to your computer and use it in GitHub Desktop.
value_if_active - django 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
@register.simple_tag(takes_context=True) | |
def value_if_active(context, return_value, view_names): | |
""" {% value_if_active 'contacts' 'class="active" %} | |
{% value_if_active 'contacts contacts/about' 'class="active" %} | |
{% value_if_active 'contacts*' 'class="active" %} - glob syntax | |
""" | |
request = context['request'] | |
try: | |
view_name = resolve(request.path).view_name | |
for vn in view_names.split(): | |
if fnmatch(view_name, vn): | |
return return_value | |
except Http404: | |
return '' | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from fnmatch import fnmatch