# Try subscripting (Normal dictionaries)
scope = scope[child]
except (TypeError, AttributeError):
- # Try the dictionary (Complex types)
- scope = scope.__dict__[child]
+ try:
+ # Try the dictionary (Complex types)
+ scope = scope.__dict__[child]
+ except:
+ # Try as a list
+ scope = scope[int(child)]
# Return an empty string if falsy, with two exceptions
# 0 should return 0, and False should return False
return scope
except AttributeError:
return scope or ''
- except (AttributeError, KeyError):
+ except (AttributeError, KeyError, IndexError):
# We couldn't find the key in the current scope
# We'll try again on the next pass
pass
self.assertEqual(result, expected)
+ # https://github.com/noahmorrison/chevron/issues/52
+ def test_indexed(self):
+ args = {
+ 'template': 'count {{count.0}}, {{count.1}}, '
+ '{{count.100}}, {{nope.0}}',
+ 'data': {
+ "count": [5,4,3,2,1],
+ }
+ }
+
+ result = chevron.render(**args)
+ expected = 'count 5, 4, , '
+
+ self.assertEqual(result, expected)
+
# Run unit tests from command line
if __name__ == "__main__":