Skip to content

Instantly share code, notes, and snippets.

@dryan
Created June 23, 2009 21:09
Show Gist options
  • Save dryan/134847 to your computer and use it in GitHub Desktop.
Save dryan/134847 to your computer and use it in GitHub Desktop.
Django templatetag for giving an input a class matching its type attr
from django.template import Library
from django.conf import settings
from django.template.defaultfilters import slugify
register = Library()
def input_class(input):
try:
slug = slugify(input.field.widget.__class__.__name__.replace('Input',''))
if hasattr(input.field.widget, 'attrs') and 'class' in input.field.widget.attrs:
slug += " " + input.field.widget.attrs['class']
return slug
except Exception, e:
return ""
register.filter(input_class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment