Skip to content

Jinja2 fails to render template with more than 98 if/elif statements #759

@dseira

Description

@dseira

Expected Behavior

No errors on rendering a template.

Actual Behavior

Having the following jinja template (this.jinja2 ) with 99 if/elif statements lines; when the template is rendered it shows the following error:

IndentationError: too many levels of indentation

On the other hand, if one of the elif statements line is removed (keeping 98 elif) the template is rendered correctly without any error.

It seems that there is a hard limit on the number of if/elif statements.

Template Code

this.jinja2

{% if name == 'server0' %}
{% elif name == 'server1' %}
{% elif name == 'server2' %}
{% elif name == 'server3' %}
....
{% elif name == 'server96' %}
{% elif name == 'server97' %}
{% elif name == 'server98' %}
{% endif %}

this.py

import os
import jinja2
def render(tpl_path, context):
    path, filename = os.path.split(tpl_path)
    return jinja2.Environment(
        loader=jinja2.FileSystemLoader(path or './')
    ).get_template(filename).render(context)

print(render('./this.jinja2', {'name': 'test'}))

Full Traceback

Traceback (most recent call last):
  File "this.py", line 9, in <module>
    print(render('./this.jinja2', {'name': 'test'}))
  File "this.py", line 7, in render
    ).get_template(filename).render(context)
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 791, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 765, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/lib/python2.7/dist-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 551, in compile
    return self._compile(source, filename)
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 512, in _compile
    return compile(source, filename, 'exec')
  File "./this.jinja2", line 499
    if 0: yield None
                   ^
IndentationError: too many levels of indentation

Your Environment

  • Python version: 2.7.6
  • Jinja version: 2.7.2-2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions