summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/templatetags/attributes.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/main/templatetags/attributes.py b/main/templatetags/attributes.py
new file mode 100644
index 00000000..bd4ccf3d
--- /dev/null
+++ b/main/templatetags/attributes.py
@@ -0,0 +1,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 value.has_key(arg):
+ 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: