Before we were only allowing subscriptable structures,
now we can take something subscriptable or more complex
like a class with attributes.
This *shouldn't* cause problems with builtins, but I'm not 100% sure.
I've checked everything I could think of.
I might want to make this opt-in at somepoint, if some people are
having issues with it.
# For every dot seperated key
for child in key.split('.'):
# Move into the scope
- scope = scope[child]
# Return the last scope we got
# or an empty string if falsy
+ try:
+ # Try subscripting (Normal dictionaries)
+ scope = scope[child]
+ except (TypeError, AttributeError):
+ # Try the dictionary (Complex types)
+ scope = scope.__dict__[child]
if scope is 0:
return 0
if scope is False:
return False
return scope or ''
- except (TypeError, KeyError):
+ except (AttributeError, KeyError):
# We couldn't find the key in the current scope
# We'll try again on the next pass
pass
self.assertEqual(result, expected)
+ def test_complex(self):
+ class Complex:
+ def __init__(self):
+ self.attr = 42
+
+ args = {
+ 'template': '{{comp.attr}} {{int.attr}}',
+ 'data': {'comp': Complex(),
+ 'int': 1
+ }
+ }
+
+ result = chevron.render(**args)
+ expected = '42 '
+
+ self.assertEqual(result, expected)
+
# Run unit tests from command line
if __name__ == "__main__":