summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 09:07:08 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 09:07:08 +0100
commitdcd0cb73120e2d924e7064933cefd3246f3cfffa (patch)
tree32f01370cfd63ca36d497ee138a12fe63e1abdfd
parent03bea567c707509c88971b76666f69749bc53469 (diff)
downloadjinja2-dcd0cb73120e2d924e7064933cefd3246f3cfffa.tar.gz
Changed lookup logic for make_attrgetter to support integers like the regular syntax
-rw-r--r--jinja2/filters.py8
-rw-r--r--jinja2/testsuite/filters.py8
2 files changed, 14 insertions, 2 deletions
diff --git a/jinja2/filters.py b/jinja2/filters.py
index 2f6ffd0..49e82aa 100644
--- a/jinja2/filters.py
+++ b/jinja2/filters.py
@@ -54,13 +54,17 @@ def environmentfilter(f):
def make_attrgetter(environment, attribute):
"""Returns a callable that looks up the given attribute from a
passed object with the rules of the environment. Dots are allowed
- to access attributes of attributes.
+ to access attributes of attributes. Integer parts in paths are
+ looked up as integers.
"""
- if not isinstance(attribute, string_types) or '.' not in attribute:
+ if not isinstance(attribute, string_types) \
+ or ('.' not in attribute and not attribute.isdigit()):
return lambda x: environment.getitem(x, attribute)
attribute = attribute.split('.')
def attrgetter(item):
for part in attribute:
+ if part.isdigit():
+ part = int(part)
item = environment.getitem(item, part)
return item
return attrgetter
diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py
index 8a6ff71..5219f76 100644
--- a/jinja2/testsuite/filters.py
+++ b/jinja2/testsuite/filters.py
@@ -256,6 +256,14 @@ class FilterTestCase(JinjaTestCase):
{'real': {'value': 18}},
]) == '42'
+ def test_sum_attributes_tuple(self):
+ tmpl = env.from_string('''{{ values.items()|sum('1') }}''')
+ assert tmpl.render(values={
+ 'foo': 23,
+ 'bar': 1,
+ 'baz': 18,
+ }) == '42'
+
def test_abs(self):
tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
assert tmpl.render() == '1|1', tmpl.render()