From e6758f1f37c2a6f06a7620fe65750cdaa5acb7e6 Mon Sep 17 00:00:00 2001 From: Jason Sun Date: Wed, 28 Mar 2018 09:20:48 +0800 Subject: [PATCH 1/2] Update base.py fix bug that it throws exception when some field not exists in context. --- microtemplates/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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) From 045401d25539b013a18213e12f7ad856e2a14f88 Mon Sep 17 00:00:00 2001 From: Jason Sun Date: Wed, 28 Mar 2018 09:27:22 +0800 Subject: [PATCH 2/2] Update tests.py append a test case for testing some missing field syntax in IF command. --- microtemplates/tests.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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()