Add a remove_item template filter.
authorMatthijs Kooijman <matthijs@stdin.nl>
Fri, 16 Jan 2009 21:15:56 +0000 (22:15 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Fri, 16 Jan 2009 21:15:56 +0000 (22:15 +0100)
The filter can be used to remove items from lists or dicts.

tools/templatetags/misc.py [new file with mode: 0644]

diff --git a/tools/templatetags/misc.py b/tools/templatetags/misc.py
new file mode 100644 (file)
index 0000000..2579295
--- /dev/null
@@ -0,0 +1,20 @@
+from django import template
+
+"""
+    Miscellaneous template tags and filters.
+"""
+
+register = template.Library()
+@register.filter(name='remove_item')
+def remove_item(container, item):
+    """
+    Removes the given user from the filtered list or dict.
+    """
+    if (item in container):
+        if isinstance(container, list):
+            container.remove(item)
+        elif isinstance(container, dict):
+            container.pop(item)
+    return container
+
+# vim: set sts=4 sw=4 expandtab: