the environment template loading functions now transparently
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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
authored by Armin Ronacher · committed by Armin Ronacher · parent df2907e · Browse files · 4 files·+43 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+43 added−1 removed
- other:+43 added−1 removed
Provenance
- Commit
9165d3e- Tree
a392a41- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
df2907e
4 files changed+43 added−1 removed
modified:docs/templates.rst+24 added
407 unmodified lines408 409 When overriding a block the `scoped` modifier does not have to be provided.410 411 412+Template Objects413+~~~~~~~~~~~~~~~~414+415+.. versionchanged:: 2.4416+417+If a template object was passed to the template context you can418+extend from that object as well. Assuming the calling code passes419+a layout template as `layout_template` to the environment, this420+code works::421+422+ {% extends layout_template %}423+424+Previously the `layout_template` variable had to be a string with425+the layout template's filename for this to work.426+427+428 HTML Escaping429 -------------430 431 When generating HTML from templates, there's always a risk that a variable will359 unmodified lines791 792 {% include ['page_detailed.html', 'page.html'] %}793 {% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}794 795+.. versionchanged:: 2.4796+ If a template object was passed to the template context you can797+ include that object using `include`.798+799 .. _import:800 801 Import802 ~~~~~~46 unmodified lines849 850 Macros and variables starting with one ore more underscores are private and851 cannot be imported.852 853+.. versionchanged:: 2.4854+ If a template object was passed to the template context you can855+ import from that object.856 857+858 .. _import-visibility:859 860 Import Context Behavior861 -----------------------449 unmodified linesmodified:jinja2/testsuite/api.py+8 added-1 removed
15 unmodified lines16 from jinja2.testsuite import JinjaTestCase17 18 from jinja2 import Environment, Undefined, DebugUndefined, \19 StrictUndefined, UndefinedError, Template, meta, \20- is_undefined20+ is_undefined, Template21 from jinja2.utils import Cycler22 23 env = Environment()24 43 unmodified lines68 69 expr = env.compile_expression("42 + foo")70 assert expr(foo=42) == 8471 72+ def test_template_passthrough(self):73+ t = Template('Content')74+ assert env.get_template(t) is t75+ assert env.select_template([t]) is t76+ assert env.get_or_select_template([t]) is t77+ assert env.get_or_select_template(t) is t78 79+80 class MetaTestCase(JinjaTestCase):81 82 def test_find_undeclared_variables(self):83 ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')117 unmodified linesmodified:jinja2/environment.py+6 added
550 unmodified lines551 552 If the template does not exist a :exc:`TemplateNotFound` exception is553 raised.554 """555+ if isinstance(name, Template):556+ return name557 if parent is not None:558 name = self.join_path(name, parent)559 return self._load_template(name, self.make_globals(globals))560 9 unmodified lines570 raise TemplatesNotFound(message=u'Tried to select from an empty list '571 u'of templates.')572 globals = self.make_globals(globals)573 for name in names:574+ if isinstance(name, Template):575+ return name576 if parent is not None:577 name = self.join_path(name, parent)578 try:579 return self._load_template(name, globals)11 unmodified lines591 .. versionadded:: 2.3592 """593 if isinstance(template_name_or_list, basestring):594 return self.get_template(template_name_or_list, parent, globals)595+ elif isinstance(template_name_or_list, Template):596+ return template_name_or_list597 return self.select_template(template_name_or_list, parent, globals)598 599 def from_string(self, source, globals=None, template_class=None):600 """Load a template from a string. This parses the source given and338 unmodified linesmodified:CHANGES+5 added
3 unmodified lines4 Version 2.45 -----------6 (codename to be selected, release date to be decided)7 8+- the environment template loading functions now transparently9+ pass through a template object if it was passed to it. This10+ makes it possible to import or extend from a template object11+ that was passed to the template.12+13 Version 2.314 -----------15 (3000 Pythons, released on February 10th 2010)16 134 unmodified lines