Skip to main content

A jinja2 extension for includes with correct indentation

Project description

# 🦸‍♂️ Not the solution `jinja2` deserves, but the workaround it needs right now.
`jinja2_workarounds` offers an extension for jinja2 that works around a long standing issue[^1]
where `include` does not preserve correct indentation for multi-line includes. Simply add the
`jinja2_workarounds.MultiLineInclude` [extension to your environment](https://jinja.palletsprojects.com/en/3.0.x/extensions/) and use the `indent content` directive to
correctly indent your multi-line includes.

## Installation
```pip install jinja2_workarounds```


## Usage example
```jinja2
# text.j2
this
is
some
text
```

```jinja2
# example.j2
example:
{% include 'text.j2' indent content %}
```

is then rendered as

```
example:
this
is
some
text
```

compared to `jinja2`'s default `include` which would result in

```
example:
this
is
some
text
```

## Advanced features
`MultiLineInclude` is compatible with custom `block_start_string` and `block_end_string`. It also works with
the advanced features of `jinja2'`s `include` statement. The following variants are all supported and work as
expected

```jinja2
{% include 'missing.j2' ignore missing indent content %} # handle missing templates
{% include ['foo.j2', 'bar.j2'] indent content %} # multiple alternative templates
{% include 'child.j2' without context %} # include child with/without content
{%- include 'child.j2' +%} # include with custom whitespace control
```

[^1]: https://github.com/pallets/jinja/issues/178

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2_workarounds-0.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

jinja2_workarounds-0.1.0-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_workarounds-0.1.0.tar.gz.

File metadata

  • Download URL: jinja2_workarounds-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.8 Darwin/20.6.0

File hashes

Hashes for jinja2_workarounds-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e65457b3dab2a1601ba8b5987f03ac890c426cd40d3bb18603a8a03322a21afb
MD5 b101b7ef205de59f3e06d5d6af5977af
BLAKE2b-256 b17658edb886a14ef80768393cd2cd704ad3a2bcdcd715417ca4831fc68f5594

See more details on using hashes here.

File details

Details for the file jinja2_workarounds-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_workarounds-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04c772eeb10975ad638d960a5d4eddc35bab00d8d0ab7695d8d540646d2692bf
MD5 7be0356309d9880c755f38758be3fde9
BLAKE2b-256 a6829a4a3d0f13720269f634e927356ed6d87fb03c4263b3945a151fe177bea1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page