From: Matthijs Kooijman Date: Tue, 13 Jan 2009 19:01:09 +0000 (+0100) Subject: Add list_or_value template filter. X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=78f556d0489510dc4b4cbcdb1980d2d2e7343b44;p=matthijs%2Fprojects%2Fxerxes.git 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. --- 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 +