executed under some conditions even though they were not part of a visible
area.
+- added `sort` filter that works like `dictsort` but for arbitrary sequences.
+
Version 2.0
-----------
(codename jinjavitus, released on July 17th 2008)
def do_dictsort(value, case_sensitive=False, by='key'):
- """ Sort a dict and yield (key, value) pairs. Because python dicts are
+ """Sort a dict and yield (key, value) pairs. Because python dicts are
unsorted you may want to use this function to order them by either
key or value:
'"key" or "value"')
def sort_func(item):
value = item[pos]
- if isinstance(value, basestring):
- value = unicode(value)
- if not case_sensitive:
- value = value.lower()
+ if isinstance(value, basestring) and not case_sensitive:
+ value = value.lower()
return value
return sorted(value.items(), key=sort_func)
+def do_sort(value, case_sensitive=False):
+ """Sort an iterable. If the iterable is made of strings the second
+ parameter can be used to control the case sensitiveness of the
+ comparison which is disabled by default.
+
+ .. sourcecode:: jinja
+
+ {% for item in iterable|sort %}
+ ...
+ {% endfor %}
+ """
+ if not case_sensitive:
+ def sort_func(item):
+ if isinstance(item, basestring):
+ item = item.lower()
+ return item
+ else:
+ sort_func = None
+ return sorted(seq, key=sort_func)
+
+
def do_default(value, default_value=u'', boolean=False):
"""If the value is undefined it will return the passed default value,
otherwise the value of the variable:
'join': do_join,
'count': len,
'dictsort': do_dictsort,
+ 'sort': do_sort,
'length': len,
'reverse': do_reverse,
'center': do_center,
{{ grouper }}: {{ list|join(', ') }}
{% endfor %}'''
FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}'''
+SORT = '''{{ ['foo', 'Bar', 'blah']|sort }}'''
def test_capitalize(env):
assert tmpl.render() == '<div>foo</div>'
tmpl = env.from_string('{{ "<div>foo</div>" }}')
assert tmpl.render() == '<div>foo</div>'
+
+
+def test_sort(env):
+ assert env.from_string(SORT).render() == "['Bar', 'blah', 'foo']"