Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

jinja2-indent

A Jinja2 extension for managing indentation in templates.

PyPI Python Build Status License

Compatibility

  • python >= 3.10
  • jinja2 >= 3.1

Installation

Install the latest release with pip:

pip install jinja2-indent

Usage

Add the extension to your Jinja2 environment:

from jinja2 import Environment
from jinja2_indent import IndentExtension

env = Environment(extensions=[IndentExtension])

{% indent %} tag

The tag adjusts the base indentation of the block content to the target column. Relative offsets between lines are preserved: a line that was indented N columns more than the current base indent stays N columns more than the new base indent.

The argument can be:

  • an integer — an absolute target column ({% indent 4 %});
  • a quoted string "+N" / "-N" — a shift relative to the current base indent ({% indent "+2" %}).

An unquoted -2 is an absolute (and therefore invalid) target: use "-2" for a relative shift.

Only leading whitespace is affected. Blank and whitespace-only lines are stripped of leading spaces and tabs, so increasing the indentation never adds trailing whitespace to empty lines.

Increasing indentation

root:
{% indent 2 -%}
- name: a
  value: 1
- name: b
  value: 2
- name: c
  value: 3
{%- endindent %}
root:
  - name: a
    value: 1
  - name: b
    value: 2
  - name: c
    value: 3

Removing indentation (relative shift)

- name: a
  value: 1

{% if need_b %}{% indent "-2" -%}
  - name: b
    value: 2
{%- endindent %}{% endif %}

- name: c
  value: 3
- name: a
  value: 1

- name: b
  value: 2

- name: c
  value: 3

Real-world example

services:
  {%- for name, service in services.items() %}{% indent "-2" %}
    {{ name }}:
      image: {{ service.image }}
      {%- if service.ports %}{% indent "-2" %}
        ports:
          {%- for port in service.ports %}
          - {{ port }}
          {%- endfor %}
      {%- endindent %}{% endif %}
  {%- endindent %}{% endfor %}

networks:
  default:
    name: {{ network_name }}

With

context = {
    "services": {
        "web": {"image": "nginx:1.27", "ports": [80, 443]},
        "worker": {"image": "worker:latest", "ports": []},
    },
    "network_name": "app-net",
}

the template renders to:

services:
  web:
    image: nginx:1.27
    ports:
      - 80
      - 443
  worker:
    image: worker:latest

networks:
  default:
    name: app-net

nindent filter

A Helm-style filter: prepends a newline and indents every non-blank line by the given number of spaces. The first line is always indented; blank lines are not.

This is handy together with Jinja2's whitespace control:

key:
  {{- "- a\n- b" | nindent(2) }}
key:
  - a
  - b

Tab size

The {% indent %} tag accepts an optional tab_size argument, which controls how a leading tab is expanded to columns when computing the current indentation. It defaults to 4 and is only relevant for templates that use tabs for indentation; space-only templates do not need it.

{% indent "-4", tab_size=8 -%}
	- name: a
{%- endindent %}

When the tag is used many times in a template, repeating tab_size is inconvenient. Set the indent__tab_size template variable once instead:

{% set indent__tab_size = 8 %}
{% indent "-4" -%}
	- name: a
{%- endindent %}

Metadata

Release files for jinja2-indent 1.0.0b1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja2-indent 1.0.0b1
File Size Uploaded Provenance Actions
jinja2_indent-1.0.0b1.tar.gz 6.0 kB Without provenance Details for jinja2_indent-1.0.0b1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-indent 1.0.0b1
File Interpreter ABI Platform Actions
jinja2_indent-1.0.0b1-py3-none-any.whl Python 3 none any Details for jinja2_indent-1.0.0b1-py3-none-any.whl

Total release size: 12.5 kB

Release files / jinja2_indent-1.0.0b1.tar.gz

Download URL jinja2_indent-1.0.0b1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
81d355d9ddc9d8bcd72df10855c8f20cf0c9d99e676a6d9e07330658af3bd773
BLAKE2b-256 checksum
How to use checksums
fbd50f30953013b2bdb10412857595ceb8e4c948bf5309218b55c5547766dbb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.12.15 Linux/7.0.0-1017-azure

Release files / jinja2_indent-1.0.0b1-py3-none-any.whl

Download URL jinja2_indent-1.0.0b1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb94e728b6b77d9d507d39f68f2a51a3eddecb0a8bc775ffbd29072ef927d5b6
BLAKE2b-256 checksum
How to use checksums
26bf96cd96f3887b5c05ec5a707710b16ae0b07816359d4f5f75eaeb702c890a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.12.15 Linux/7.0.0-1017-azure

Security for jinja2-indent 1.0.0b1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page