Fix: original template was partial overwritten, thus the rendered output was never updated

This commit is contained in:
s3lph 2019-11-29 23:44:43 +01:00
parent b9269146e1
commit 50508b2328
2 changed files with 18 additions and 6 deletions

View file

@ -43,14 +43,10 @@ def render_traverse(obj):
"""
if isinstance(obj, list):
# list -> recurse into each item
for i in range(len(obj)):
obj[i] = render_traverse(obj[i])
return obj
return [render_traverse(x) for x in obj]
elif isinstance(obj, dict):
# dict -> recurse into the value of each (key, value)
for k, v in obj.items():
obj[k] = render_traverse(obj[k])
return obj
return {k: render_traverse(v) for k, v in obj.items()}
elif isinstance(obj, str):
# str -> template
return render(obj)

View file

@ -22,6 +22,15 @@ class TemplateTest(unittest.TestCase):
def template_test_test(value):
return value == 'baz'
@staticmethod
@template_function
def template_test_function_nocache():
TemplateTest.counter1 += 1
return TemplateTest.counter1
def setUp(self) -> None:
TemplateTest.counter1 = 0
def test_template(self):
env = _env_init()
self.assertEqual('"not a template"', env.from_string('not a template').render())
@ -60,3 +69,10 @@ class TemplateTest(unittest.TestCase):
self.assertEqual('test_other_bar', rendered['test_functions']['test_filter'])
self.assertTrue(rendered['test_functions']['test_test_true'])
self.assertFalse(rendered['test_functions']['test_test_false'])
def test_no_cache(self):
template = ['{{ template_test_function_nocache() }}']
r1 = render_traverse(template)
r2 = render_traverse(template)
self.assertEqual(1, r1[0])
self.assertEqual(2, r2[0])