]> Devi Nivas Git - chevron.git/commitdiff
Implement indexed scopes #52
authorDaniel Morrison <dan@morrison.ph>
Tue, 16 Apr 2019 23:03:15 +0000 (19:03 -0400)
committerDaniel Morrison <dan@morrison.ph>
Tue, 16 Apr 2019 23:03:15 +0000 (19:03 -0400)
chevron/renderer.py
test_spec.py

index dd9c1522c714c5d29f781e153d3d447db193b932..0d0d4b971310cf0c5efdfd0d449794fa3fb68449 100644 (file)
@@ -65,8 +65,12 @@ def _get_key(key, scopes):
                     # 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
@@ -82,7 +86,7 @@ def _get_key(key, scopes):
                     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
index 40befb68ac626a7f16cbd7815935ab6c5996e615..72ef263bc26430801aad0869471e47e1715c18df 100755 (executable)
@@ -357,6 +357,21 @@ class ExpandedCoverage(unittest.TestCase):
 
         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__":