Skip to content

Instantly share code, notes, and snippets.

@smant
Created October 12, 2013 17:02
Show Gist options
  • Save smant/6952357 to your computer and use it in GitHub Desktop.
Save smant/6952357 to your computer and use it in GitHub Desktop.
value_if_active - django template tag
@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 ''
@smant
Copy link
Author

smant commented Oct 12, 2013

from fnmatch import fnmatch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment