1 from django import template
2 from django.template.defaultfilters import unordered_list
3 from django.utils.safestring import mark_safe
4 from django.utils.encoding import force_unicode
5 from django.utils.translation import ugettext as _
8 Template tags and filters for working with lists.
11 register = template.Library()
13 @register.filter(name='list_or_value')
14 def list_or_value(list, autoescape=None):
16 Turn a list into a simple string or unordered list.
18 If the list is empty, returns an empty string.
19 If the list contains one element, returns just that element.
20 If the list contains more elements, return an ordered list with
21 those elements (Just like the builtin unordered_list, but with the
29 return mark_safe('<ul>' + unordered_list(list, autoescape=autoescape) + '</ul>')
30 list_or_value.needs_autoescape = True
32 @register.filter(name='natural_list')
33 def natural_list(list):
35 Turns the list into a natural list, using comma's and "and" for
36 joining the terms. The result is somewhat localized (but probably
37 insufficient for language that use completely different
38 interpunction for lists).
43 for item in list[0:-1]:
46 res += force_unicode(item)
49 res += ' %s ' % _('and')
51 res += force_unicode(list[-1])
54 natural_list.is_safe = True