From 9165d3ed52a63c3cd41332c3b4cae0631c01aaa9 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 16 Feb 2010 17:35:59 +0100 Subject: [PATCH] the environment template loading functions now transparently pass through a template object if it was passed to it. This makes it possible to import or extend from a template object that was passed to the template. --HG-- branch : trunk --- CHANGES | 5 +++++ docs/templates.rst | 24 ++++++++++++++++++++++++ jinja2/environment.py | 6 ++++++ jinja2/testsuite/api.py | 9 ++++++++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 8d1b8b6..26b386e 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,11 @@ Version 2.4 ----------- (codename to be selected, release date to be decided) +- the environment template loading functions now transparently + pass through a template object if it was passed to it. This + makes it possible to import or extend from a template object + that was passed to the template. + Version 2.3 ----------- (3000 Pythons, released on February 10th 2010) diff --git a/docs/templates.rst b/docs/templates.rst index 06a339b..087fb59 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -409,6 +409,22 @@ modifier to a block declaration:: When overriding a block the `scoped` modifier does not have to be provided. +Template Objects +~~~~~~~~~~~~~~~~ + +.. versionchanged:: 2.4 + +If a template object was passed to the template context you can +extend from that object as well. Assuming the calling code passes +a layout template as `layout_template` to the environment, this +code works:: + + {% extends layout_template %} + +Previously the `layout_template` variable had to be a string with +the layout template's filename for this to work. + + HTML Escaping ------------- @@ -776,6 +792,10 @@ Example:: {% include ['page_detailed.html', 'page.html'] %} {% include ['special_sidebar.html', 'sidebar.html'] ignore missing %} +.. versionchanged:: 2.4 + If a template object was passed to the template context you can + include that object using `include`. + .. _import: Import @@ -830,6 +850,10 @@ namespace:: Macros and variables starting with one ore more underscores are private and cannot be imported. +.. versionchanged:: 2.4 + If a template object was passed to the template context you can + import from that object. + .. _import-visibility: diff --git a/jinja2/environment.py b/jinja2/environment.py index e3c64e3..4e9e4fd 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -552,6 +552,8 @@ class Environment(object): If the template does not exist a :exc:`TemplateNotFound` exception is raised. """ + if isinstance(name, Template): + return name if parent is not None: name = self.join_path(name, parent) return self._load_template(name, self.make_globals(globals)) @@ -569,6 +571,8 @@ class Environment(object): u'of templates.') globals = self.make_globals(globals) for name in names: + if isinstance(name, Template): + return name if parent is not None: name = self.join_path(name, parent) try: @@ -588,6 +592,8 @@ class Environment(object): """ if isinstance(template_name_or_list, basestring): return self.get_template(template_name_or_list, parent, globals) + elif isinstance(template_name_or_list, Template): + return template_name_or_list return self.select_template(template_name_or_list, parent, globals) def from_string(self, source, globals=None, template_class=None): diff --git a/jinja2/testsuite/api.py b/jinja2/testsuite/api.py index 2b16fac..194e9a1 100644 --- a/jinja2/testsuite/api.py +++ b/jinja2/testsuite/api.py @@ -17,7 +17,7 @@ from jinja2.testsuite import JinjaTestCase from jinja2 import Environment, Undefined, DebugUndefined, \ StrictUndefined, UndefinedError, Template, meta, \ - is_undefined + is_undefined, Template from jinja2.utils import Cycler env = Environment() @@ -69,6 +69,13 @@ class ExtendedAPITestCase(JinjaTestCase): expr = env.compile_expression("42 + foo") assert expr(foo=42) == 84 + def test_template_passthrough(self): + t = Template('Content') + assert env.get_template(t) is t + assert env.select_template([t]) is t + assert env.get_or_select_template([t]) is t + assert env.get_or_select_template(t) is t + class MetaTestCase(JinjaTestCase): -- 2.26.2