summaryrefslogtreecommitdiffstats
path: root/main/templatetags/attributes.py
blob: ab09b63e18dba4730dd1ddcaff3f13121e9a3557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re
from django import template
from django.conf import settings

numeric_test = re.compile("^\d+$")
register = template.Library()

def attribute(value, arg):
    """Gets an attribute of an object dynamically from a string name"""
    if hasattr(value, str(arg)):
        return getattr(value, arg)
    elif hasattr(value, 'has_key') and arg in value:
        return value[arg]
    elif numeric_test.match(str(arg)) and len(value) > int(arg):
        return value[int(arg)]
    else:
        return settings.TEMPLATE_STRING_IF_INVALID

register.filter('attribute', attribute)

# vim: set ts=4 sw=4 et: