Jinja keep indentation on include or macro

One way is to wrap the include in a macro, then because the macro is a function, its output can be passed through the indent filter:

class MyClass:
    def someOp():
        pass

    {% macro someop() %}{% include "someOp.html" %}{% endmacro %}
    {{ someop()|indent }}

By default ‘indent’ indents 4 spaces and does not indent the first line, you can use e.g. ‘indent(8)’ to indent further, see http://jinja.pocoo.org/docs/templates/#list-of-builtin-filters for more details.

If what you’re including is defined as a macro to begin with then the further wrapper macro is not needed, and you can jump straight to using the indent filter.

Leave a Comment