diff --git a/microtemplates/base.py b/microtemplates/base.py index 15b3c69..947b852 100644 --- a/microtemplates/base.py +++ b/microtemplates/base.py @@ -66,7 +66,10 @@ def resolve(name, context): name = name[2:] try: for tok in name.split('.'): - context = context[tok] + if tok in context: + context = context[tok] + else: + return None return context except KeyError: raise TemplateContextError(name) diff --git a/microtemplates/tests.py b/microtemplates/tests.py index 1f62515..8452a5c 100644 --- a/microtemplates/tests.py +++ b/microtemplates/tests.py @@ -68,6 +68,10 @@ def test_truthy_thingy(self): self.assertEquals(rendered, '') rendered = Template('{% if items %}we have items{% end %}').render(items=[1]) self.assertEquals(rendered, 'we have items') + + def test_some_field_does_not_exist(self): + rendered = Template('{% if context.has_flag %}we have items{% else %}we have nothing{% end %}').render(context={"items":[1,2]}) + self.assertEquals(rendered, 'we have nothing') def pow(m=2, e=2): @@ -96,4 +100,4 @@ def test_keyword_args(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()