Permalink
Browse files

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
  • Loading branch information...
1 parent df2907e commit 9165d3ed52a63c3cd41332c3b4cae0631c01aaa9 @mitsuhiko mitsuhiko committed Feb 16, 2010
Showing with 43 additions and 1 deletion.
  1. +5 −0 CHANGES
  2. +24 −0 docs/templates.rst
  3. +6 −0 jinja2/environment.py
  4. +8 −1 jinja2/testsuite/api.py
View
@@ -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)
View
@@ -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:
View
@@ -552,6 +552,8 @@ def get_template(self, name, parent=None, globals=None):
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 @@ def select_template(self, names, parent=None, globals=None):
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 @@ def get_or_select_template(self, template_name_or_list,
"""
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):
View
@@ -17,7 +17,7 @@
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 @@ def test_expressions(self):
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):

0 comments on commit 9165d3e

Please sign in to comment.