Jinja2 extension that redirects output to output file(s)
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinja2_outputfile-0.5.1.tar.gz.
File metadata
- Download URL: jinja2_outputfile-0.5.1.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2bd953a0881ed747aa934a0fc64cfb710f15e12127be7e73a2472172705a4a0
|
|
| MD5 |
7662c29ec55fb4e6654bb4083e53f212
|
|
| BLAKE2b-256 |
aca4baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137
|
Provenance
The following attestation bundles were made for jinja2_outputfile-0.5.1.tar.gz:
Publisher:
release-to-pypi.yml on jenisys/jinja2-outputfile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_outputfile-0.5.1.tar.gz -
Subject digest:
c2bd953a0881ed747aa934a0fc64cfb710f15e12127be7e73a2472172705a4a0 - Sigstore transparency entry: 381894847
- Sigstore integration time:
-
Permalink:
jenisys/jinja2-outputfile@c14512f3ad3f847c67af5460e68ac2a8311622f1 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/jenisys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-to-pypi.yml@c14512f3ad3f847c67af5460e68ac2a8311622f1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file jinja2_outputfile-0.5.1-py3-none-any.whl.
File metadata
- Download URL: jinja2_outputfile-0.5.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e9a969ff5d26a1cb490baa34ae0ca30640e97c0844f45d9e7531226242f6750
|
|
| MD5 |
289cf09444835ab4eb3468ad87f46b5f
|
|
| BLAKE2b-256 |
b99af050d449623d478725a5989905a46bcd2ca3046e5bbcb923c07714d8217f
|
Provenance
The following attestation bundles were made for jinja2_outputfile-0.5.1-py3-none-any.whl:
Publisher:
release-to-pypi.yml on jenisys/jinja2-outputfile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_outputfile-0.5.1-py3-none-any.whl -
Subject digest:
2e9a969ff5d26a1cb490baa34ae0ca30640e97c0844f45d9e7531226242f6750 - Sigstore transparency entry: 381894859
- Sigstore integration time:
-
Permalink:
jenisys/jinja2-outputfile@c14512f3ad3f847c67af5460e68ac2a8311622f1 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/jenisys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-to-pypi.yml@c14512f3ad3f847c67af5460e68ac2a8311622f1 -
Trigger Event:
release
-
Statement type: