Skip to main content

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


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.0.tar.gz (11.8 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.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_outputfile-0.5.0.tar.gz.

File metadata

  • Download URL: jinja2_outputfile-0.5.0.tar.gz
  • Upload date:
  • Size: 11.8 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.0.tar.gz
Algorithm Hash digest
SHA256 8550668a1af55acf82d24dc94e9cad348d5768a0e0649ddd65ad12313263f63f
MD5 647ea587c53124996fed762d9d4c24c6
BLAKE2b-256 0632ed32d9ea0769e67d80892132d80fbe3ce005164e1ca4bb1e3a3a72820630

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_outputfile-0.5.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_outputfile-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cbe080cb849f9eaecf78d8ac8fd532056d4687f3bde81ac522fbf05d49921b3
MD5 9da7b59de8760367b20b5253ee16d0e6
BLAKE2b-256 f52c30b30cddb840639c304a476569f6a776ea542c173e968376c1b2b6b7fa3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_outputfile-0.5.0-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