Outputfile Extension for the Jinja2 Template Engine
Provides a Jinja2 directive/extension that supports to redirect rendered template part(s) to output-file(s).
USE CASES:
- Redirect a rendered Jinja2 text template part to an output-file
- Use a Jinja2 template as build script if many output-files should be generated from the same data model
FEATURES:
- Output suppression: Output files are only written, if the file contents have changed.
- Verbose mode: Prints which output-files are written and verdict (
CHANGED,SAME). - Quiet mode: Verbose mode output is suppressed (use:
jinja_outputfile.QuietOutputFileExtension)
EXAMPLE 1: Write rendered text to an output-file
# -- FILE: example_one.py
# Redirect rendered text from inside the outputfile block to an output-file.
from jinja2 import Environment
from pathlib import Path
template_as_text = """\
{% outputfile "%s/example_%s.txt"|format(this.output_dir, this.name) -%}
Hello {{this.name}}
{%- endoutputfile %}
"""
# -- EXAMPLE: Using the template
env = Environment(extensions=["jinja2_outputfile.Extension"])
template = env.from_string(template_as_text)
template.render(this=dict(name="Alice", output_dir="."))
# -- POSTCONDITION: FILE WAS CREATED/WRITTEN (with contents)
output_file = Path(".")/"example_Alice.txt"
output_file_contents = output_file.read_text()
assert output_file.exists()
assert output_file_contents == "Hello Alice\n"
EXAMPLE 2: Use multiple output-files
# -- FILE: example.py
from jinja2 import Environment
from pathlib import Path
THIS_TEMPLATE = """
{%- for name in this.names -%}
{% outputfile "%s/example_%s.txt"|format(this.output_dir, name) -%}
Hello {{name}}
{%- endoutputfile %}
{% endfor %}
"""
# -- EXAMPLE: Using the template
env = Environment(extensions=["jinja2_outputfile.Extension"])
code_generator = env.from_string(THIS_TEMPLATE)
code_generator.render(this=dict(names=["Alice", "Bob"], output_dir="."))
# -- POSTCONDITION: FILES were WRITTEN (with contents)
output_file1 = Path(".")/"example_Alice.txt"
output_file2 = Path(".")/"example_Bob.txt"
assert output_file1.exists()
assert output_file2.exists()
assert output_file1.read_text() == "Hello Alice\n"
assert output_file2.read_text() == "Hello Bob\n"
Rationale
The outputfile directive is useful in a code generator use cases
if many output files need to be generated from Jinja2 templates.
In this case, you can provide one template as control script to accomplish this task.
History
- INITIALLY CREATED AS:
simplegen.jinja2_ext.outputfile - REFACTORING: Extracted into own standalone package to simplify reuse with Jinja2 template engine.
Metadata
Release files for jinja2-outputfile 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jinja2_outputfile-0.5.1.tar.gz | 13.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jinja2_outputfile-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.6 kB
Release files / jinja2_outputfile-0.5.1.tar.gz
| Download URL | jinja2_outputfile-0.5.1.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2bd953a0881ed747aa934a0fc64cfb710f15e12127be7e73a2472172705a4a0
|
|
BLAKE2b-256 checksum How to use checksums |
aca4baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2025.
Transparency logRelease files / jinja2_outputfile-0.5.1-py3-none-any.whl
| Download URL | jinja2_outputfile-0.5.1-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2e9a969ff5d26a1cb490baa34ae0ca30640e97c0844f45d9e7531226242f6750
|
|
BLAKE2b-256 checksum How to use checksums |
b99af050d449623d478725a5989905a46bcd2ca3046e5bbcb923c07714d8217f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2025.
Transparency log