Skip to main content

Jinja2 extension that redirects output to output file(s)

Project description

Outputfile Extension for the Jinja2 Template Engine

CI Build Status Latest Version License

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


Download files

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

Source Distribution

jinja2_outputfile-0.5.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinja2_outputfile-0.5.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

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

Hashes for jinja2_outputfile-0.5.1.tar.gz
Algorithm Hash digest
SHA256 c2bd953a0881ed747aa934a0fc64cfb710f15e12127be7e73a2472172705a4a0
MD5 7662c29ec55fb4e6654bb4083e53f212
BLAKE2b-256 aca4baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_outputfile-0.5.1.tar.gz:

Publisher: release-to-pypi.yml on jenisys/jinja2-outputfile

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jinja2_outputfile-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_outputfile-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e9a969ff5d26a1cb490baa34ae0ca30640e97c0844f45d9e7531226242f6750
MD5 289cf09444835ab4eb3468ad87f46b5f
BLAKE2b-256 b99af050d449623d478725a5989905a46bcd2ca3046e5bbcb923c07714d8217f

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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