summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
eb2b5fc)
This template filter transforms lists like ['foo', 'bar', 'baz'] into a
natural list like 'foo, bar and baz'.
from django import template
from django.template.defaultfilters import unordered_list
from django.utils.safestring import mark_safe
from django import template
from django.template.defaultfilters import unordered_list
from django.utils.safestring import mark_safe
+from django.utils.encoding import force_unicode
+from django.utils.translation import ugettext as _
"""
Template tags and filters for working with lists.
"""
Template tags and filters for working with lists.
else:
return mark_safe('<ul>' + unordered_list(list, autoescape=autoescape) + '</ul>')
list_or_value.needs_autoescape = True
else:
return mark_safe('<ul>' + unordered_list(list, autoescape=autoescape) + '</ul>')
list_or_value.needs_autoescape = True
+
+@register.filter(name='natural_list')
+def natural_list(list):
+ """
+ Turns the list into a natural list, using comma's and "and" for
+ joining the terms. The result is somewhat localized (but probably
+ insufficient for language that use completely different
+ interpunction for lists).
+ """
+ if len(list) == 0:
+ return ''
+ res = ''
+ for item in list[0:-1]:
+ if res:
+ res += ', '
+ res += item.__unicode__()
+
+ if res:
+ res += ' %s ' % _('and')
+
+ res += list[-1].__unicode__()
+
+ return res
+natural_list.is_safe = True