Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stereobutter/jinja2_workarounds
A jinja2 extension for includes with correct indentation
https://github.com/stereobutter/jinja2_workarounds
jinja2 jinja2-extension
Last synced: 8 days ago
JSON representation
A jinja2 extension for includes with correct indentation
- Host: GitHub
- URL: https://github.com/stereobutter/jinja2_workarounds
- Owner: stereobutter
- License: mit
- Created: 2022-03-14T10:30:36.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-31T08:04:31.000Z (about 2 years ago)
- Last Synced: 2024-10-12T06:23:16.468Z (24 days ago)
- Topics: jinja2, jinja2-extension
- Language: Python
- Homepage:
- Size: 13.7 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π¦ΈββοΈ 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