Skip to content

lstrip_blocks seems dead and doesn’t work #2074

@avibrazil

Description

@avibrazil

The lstrip_blocks doesn’t work as documented and seems to have no effect at all. I need it to help me attain more readable code and templates while keeping full control on rendered text.

Here is a fully functional sample code:

import textwrap
import jinja2

data=[
    dict(a=1,b=2),
    dict(a=3,b=4),
]

template = jinja2.Template(
    textwrap.dedent("""\
        Preamble

        {% for item in data %}
            Block start
            This is a:{{ item.a }}
            This is b:{{ item.b }}
            Block end
        {% endfor %}

        Epilogue
    """),
    lstrip_blocks=True
)

print(template.render(dict(data=data)))

Actual result:

Preamble


    Block start
    This is a:1
    This is b:2
    Block end

    Block start
    This is a:3
    This is b:4
    Block end


Epilogue

Expected and desired result:

Preamble


Block start
This is a:1
This is b:2
Block end

Block start
This is a:3
This is b:4
Block end


Epilogue

Environment:

  • Python version: 3.12.8 on Fedora Linux
  • Jinja version: 3.1.5

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