summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-04-20 08:37:57 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-04-20 08:37:57 -0400
commitc05bb65048278484b59bc71f28feb846748db746 (patch)
tree935c96f60a8b3f41c115a6a94d2193eaf3adafdb
parent921add5fecbeaf18f649f7c21c26d0e0b4f8e1a7 (diff)
downloadpython-coveragepy-git-c05bb65048278484b59bc71f28feb846748db746.tar.gz
Variables' items (dict access) can be used in templates.
-rw-r--r--coverage/templite.py5
-rw-r--r--test/test_templite.py5
2 files changed, 9 insertions, 1 deletions
diff --git a/coverage/templite.py b/coverage/templite.py
index 2eab864a..f75e2a2d 100644
--- a/coverage/templite.py
+++ b/coverage/templite.py
@@ -73,7 +73,10 @@ class ContextAccess(object):
dots = key.split('.')
value = self[dots[0]]
for dot in dots[1:]:
- value = getattr(value, dot)
+ try:
+ value = getattr(value, dot)
+ except AttributeError:
+ value = value[dot]
if callable(value):
value = value()
else:
diff --git a/test/test_templite.py b/test/test_templite.py
index 15055f2e..fd7a3393 100644
--- a/test/test_templite.py
+++ b/test/test_templite.py
@@ -66,6 +66,11 @@ class TemplateTest(unittest.TestCase):
obj.txt = "Once"
self.try_render("{{obj.ditto}}", locals(), "OnceOnce")
+ def test_item_access(self):
+ # Variables' items can be used.
+ d = {'a':17, 'b':23}
+ self.try_render("{{d.a}} < {{d.b}}", locals(), "17 < 23")
+
def test_loops(self):
# Loops work like in Django.
nums = [1,2,3,4]