Include statements can now be marked with ``ignore missing`` to skip
Before adoption: this commit came in with the history the project adopted, so no landing records it.
non existing templates. --HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 92af756 · Browse files · 7 files·+58 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+58 added−10 removed
- tests:+11 added−0 removed
- other:+47 added−10 removed
Provenance
- Commit
37f58ce- Tree
49fb1d8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
92af756
7 files changed+58 added−10 removed
modified:docs/templates.rst+10 added
722 unmodified lines723 Included templates have access to the variables of the active context by724 default. For more details about context behavior of imports and includes725 see :ref:`import-visibility`.726 727+From Jinja 2.2 onwards you can mark an include with ``ignore missing`` in728+which case Jinja will ignore the statement if the template to be ignored729+does not exist. When combined with ``with`` or ``without context`` it has730+to be placed *before* the context visibility statement. Here some valid731+examples::732+733+ {% include "sidebar.html" ignore missing %}734+ {% include "sidebar.html" ignore missing with context %}735+ {% include "sidebar.html" ignore missing without context %}736+737 .. _import:738 739 Import740 ~~~~~~473 unmodified linesmodified:jinja2/compiler.py+20 added-7 removed
816 unmodified lines817 self.extends_so_far += 1818 819 def visit_Include(self, node, frame):820 """Handles includes."""821+ if node.ignore_missing:822+ self.writeline('try:')823+ self.indent()824+ self.writeline('template = environment.get_template(', node)825+ self.visit(node.template, frame)826+ self.write(', %r)' % self.name)827+ if node.ignore_missing:828+ self.outdent()829+ self.writeline('except TemplateNotFound:')830+ self.indent()831+ self.writeline('pass')832+ self.outdent()833+ self.writeline('else:')834+ self.indent()835+836 if node.with_context:822- self.writeline('template = environment.get_template(', node)823- self.visit(node.template, frame)824- self.write(', %r)' % self.name)837 self.writeline('for event in template.root_render_func('838 'template.new_context(context.parent, True, '839 'locals())):')840 else:829- self.writeline('for event in environment.get_template(', node)830- self.visit(node.template, frame)831- self.write(', %r).module._body_stream:' %832- self.name)841+ self.writeline('for event in template.module._body_stream:')842+843 self.indent()844 self.simple_write('event', frame)845 self.outdent()846 847+ if node.ignore_missing:848+ self.outdent()849+850 def visit_Import(self, node, frame):851 """Visit regular imports."""852 self.writeline('l_%s = ' % node.target, node)853 if frame.toplevel:604 unmodified linesmodified:jinja2/nodes.py+1 added-1 removed
272 unmodified lines273 274 275 class Include(Stmt):276 """A node that represents the include tag."""277- fields = ('template', 'with_context')277+ fields = ('template', 'with_context', 'ignore_missing')278 279 280 class Import(Stmt):281 """A node that represents the import tag."""503 unmodified linesmodified:jinja2/parser.py+6 added
168 unmodified lines169 170 def parse_include(self):171 node = nodes.Include(lineno=self.stream.next().lineno)172 node.template = self.parse_expression()173+ if self.stream.current.test('name:ignore') and \174+ self.stream.look().test('name:missing'):175+ node.ignore_missing = True176+ self.stream.skip(2)177+ else:178+ node.ignore_missing = False179 return self.parse_import_context(node, True)180 181 def parse_import(self):182 node = nodes.Import(lineno=self.stream.next().lineno)598 unmodified linesmodified:jinja2/runtime.py+3 added-2 removed
10 unmodified lines11 import sys12 from itertools import chain, imap13 from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \14 concat, MethodType, FunctionType15-from jinja2.exceptions import UndefinedError, TemplateRuntimeError15+from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \16+ TemplateNotFound17 18 19 # these variables are exported to the template runtime20 __all__ = ['LoopContext', 'Context', 'TemplateReference', 'Macro', 'Markup',21 'TemplateRuntimeError', 'missing', 'concat', 'escape',21- 'markup_join', 'unicode_join']22+ 'markup_join', 'unicode_join', 'TemplateNotFound']23 24 25 #: the types we support for context functions26 _context_function_types = (FunctionType, MethodType)459 unmodified linesmodified:tests/test_imports.py+11 added
4 unmodified lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+from py.test import raises10 from jinja2 import Environment, DictLoader11+from jinja2.exceptions import TemplateNotFound12 13 14 test_env = Environment(loader=DictLoader(dict(15 module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',25 unmodified lines41 t = test_env.from_string('{% include "header" without context %}')42 assert t.render(foo=42) == '[|23]'43 44 45+def test_include_ignoring_missing():46+ t = test_env.from_string('{% include "missing" %}')47+ raises(TemplateNotFound, t.render)48+ for extra in '', 'with context', 'without context':49+ t = test_env.from_string('{% include "missing" ignore missing ' +50+ extra + ' %}')51+ assert t.render() == ''52+53+54 def test_context_include_with_overrides():55 env = Environment(loader=DictLoader(dict(56 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",57 item="{{ item }}"25 unmodified linesmodified:CHANGES+7 added
1 Jinja2 Changelog2 ================3 4+Version 2.25+-----------6+(codename unknown, release date yet unknown)7+8+- Include statements can now be marked with ``ignore missing`` to skip9+ non existing templates.10+11 Version 2.1.112 -------------13 (Bugfix release)14 83 unmodified lines