Skip to content
Myr

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

7 files+58 added−10 removed

modified:docs/templates.rst+10 added
722 unmodified lines
723Included templates have access to the variables of the active context by724default. For more details about context behavior of imports and includes725see :ref:`import-visibility`.726727From Jinja 2.2 onwards you can mark an include with ``ignore missing`` in728which case Jinja will ignore the statement if the template to be ignored729does not exist. When combined with ``with`` or ``without context`` it has730to be placed *before* the context visibility statement. Here some valid731examples::732733 {% include "sidebar.html" ignore missing %}734 {% include "sidebar.html" ignore missing with context %}735 {% include "sidebar.html" ignore missing without context %}736737.. _import:738739Import740~~~~~~
473 unmodified lines
modified:jinja2/compiler.py+20 added-7 removed
816 unmodified lines
817 self.extends_so_far += 1818819 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()835836 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:')842843 self.indent()844 self.simple_write('event', frame)845 self.outdent()846847 if node.ignore_missing:848 self.outdent()849850 def visit_Import(self, node, frame):851 """Visit regular imports."""852 self.writeline('l_%s = ' % node.target, node)853 if frame.toplevel:
604 unmodified lines
modified:jinja2/nodes.py+1 added-1 removed
272 unmodified lines
273274275class Include(Stmt):276 """A node that represents the include tag."""277 fields = ('template', 'with_context')277 fields = ('template', 'with_context', 'ignore_missing')278279280class Import(Stmt):281 """A node that represents the import tag."""
503 unmodified lines
modified:jinja2/parser.py+6 added
168 unmodified lines
169170 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)180181 def parse_import(self):182 node = nodes.Import(lineno=self.stream.next().lineno)
598 unmodified lines
modified:jinja2/runtime.py+3 added-2 removed
10 unmodified lines
11import sys12from itertools import chain, imap13from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \14 concat, MethodType, FunctionType15from jinja2.exceptions import UndefinedError, TemplateRuntimeError15from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \16 TemplateNotFound171819# 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']232425#: the types we support for context functions26_context_function_types = (FunctionType, MethodType)
459 unmodified lines
modified:tests/test_imports.py+11 added
4 unmodified lines
56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from py.test import raises10from jinja2 import Environment, DictLoader11from jinja2.exceptions import TemplateNotFound121314test_env = Environment(loader=DictLoader(dict(15 module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',
25 unmodified lines
41 t = test_env.from_string('{% include "header" without context %}')42 assert t.render(foo=42) == '[|23]'434445def 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() == ''525354def 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 lines
modified:CHANGES+7 added
1Jinja2 Changelog2================34Version 2.25-----------6(codename unknown, release date yet unknown)78- Include statements can now be marked with ``ignore missing`` to skip9 non existing templates.1011Version 2.1.112-------------13(Bugfix release)14
83 unmodified lines