From 78f556d0489510dc4b4cbcdb1980d2d2e7343b44 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 13 Jan 2009 20:01:09 +0100 Subject: [PATCH] Add list_or_value template filter. This filter allows one to build an unordered list from lists with more than one element or a simple string otherwise. --- tools/templatetags/list.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tools/templatetags/list.py diff --git a/tools/templatetags/list.py b/tools/templatetags/list.py new file mode 100644 index 0000000..e0111b0 --- /dev/null +++ b/tools/templatetags/list.py @@ -0,0 +1,28 @@ +from django import template +from django.template.defaultfilters import unordered_list +from django.utils.safestring import mark_safe + +""" + Template tags and filters for working with lists. +""" + +register = template.Library() + +@register.filter(name='list_or_value') +def list_or_value(list, autoescape=None): + """ + Turn a list into a simple string or unordered list. + + If the list is empty, returns an empty string. + If the list contains one element, returns just that element. + If the list contains more elements, return an ordered list with + those elements (Just like the builtin unordered_list, but with the +