]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
fix bug with cached templates not using new globals 1244/head
authorAmy <leiamy12@gmail.com>
Tue, 23 Jun 2020 19:36:48 +0000 (15:36 -0400)
committerAmy <leiamy12@gmail.com>
Tue, 30 Mar 2021 05:43:51 +0000 (01:43 -0400)
CHANGES.rst
src/jinja2/environment.py
tests/test_regression.py

index 3ea51cb466766ef2994b693c2557a879b699418d..7c0284f80c0765f2c01f6a07e73055a739dc160f 100644 (file)
@@ -36,6 +36,8 @@ Unreleased
     being accessed in custom context functions. :issue:`768`
 -   Fix a bug that caused scoped blocks from accessing special loop
     variables. :issue:`1088`
+-   Fix a bug that prevented cached templates from registering new globals.
+    :issue:`295`
 
 
 Version 2.11.3
index ccc7837da7550c387125a6e89a21d27fa7873871..e7c42cdcc06f7e467e09f053b1753814817004e4 100644 (file)
@@ -811,6 +811,16 @@ class Environment:
             if template is not None and (
                 not self.auto_reload or template.is_up_to_date
             ):
+                # update globals if changed
+                new_globals = globals.items() - template.globals.items()
+                if new_globals:
+                    # it is possible for the template and environment to share
+                    # a globals object, in which case, a new copy should be
+                    # made to avoid affecting other templates
+                    if template.globals is self.globals:
+                        template.globals = dict(template.globals, **globals)
+                    else:
+                        template.globals.update(dict(new_globals))
                 return template
         template = self.loader.load(self, name, globals)
         if self.cache is not None:
index 716d4a06b11a59798d6a78f150c4a5fc568f76e3..945061ab5402a890c3c11216b96a3f9ecafc3791 100644 (file)
@@ -716,3 +716,37 @@ End"""
         # values set within a block or loop should not
         # show up outside of it
         assert tmpl.render() == "42\n0\n24\n0\n42\n1\n24\n1\n42"
+
+    def test_cached_extends(self):
+        env = Environment(
+            loader=DictLoader(
+                {"parent": "{{ foo }}", "child": "{% extends 'parent' %}"}
+            )
+        )
+        tmpl = env.get_template("child", globals={"foo": "bar"})
+        assert tmpl.render() == "bar"
+
+        tmpl = env.get_template("parent", globals={"foo": 42})
+        assert tmpl.render() == "42"
+
+        tmpl = env.get_template("child")
+        assert tmpl.render() == "bar"
+
+        tmpl = env.get_template("parent")
+        assert tmpl.render() == "42"
+
+    def test_cached_includes(self):
+        env = Environment(
+            loader=DictLoader({"base": "{{ foo }}", "main": "{% include 'base' %}"})
+        )
+        tmpl = env.get_template("main", globals={"foo": "bar"})
+        assert tmpl.render() == "bar"
+
+        tmpl = env.get_template("base", globals={"foo": 42})
+        assert tmpl.render() == "42"
+
+        tmpl = env.get_template("main")
+        assert tmpl.render() == "bar"
+
+        tmpl = env.get_template("base")
+        assert tmpl.render() == "42"