Metadata-Version: 2.4
Name: jinja2-outputfile
Version: 0.5.0
Summary: Jinja2 extension that redirects output to output file(s)
Home-page: https://github.com/jenisys/jinja2-outputfile
Download-URL: https://pypi.org/project/jinja2-outputfile
Author: Jens Engel
Author-email: Jens Engel <jenisys@noreply.github.com>
License-Expression: BSD-3-Clause
Project-URL: Homepage, https://github.com/jenisys/jinja2-outputfile
Project-URL: Repository, https://github.com/jenisys/jinja2-outputfile
Project-URL: Download, https://pypi.org/project/jinja2-outputfile
Project-URL: Issues, https://github.com/jenisys/jinja2-outputfile/issues/
Keywords: Jinja2,jinja2,template-extension
Platform: any
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Code Generators
Requires-Python: !=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7
Description-Content-Type: text/markdown
Requires-Dist: jinja2>=3.0; python_version >= "3.6"
Requires-Dist: jinja2<3.0; python_version < "3.6"
Requires-Dist: pathlib2; python_version <= "3.4"
Dynamic: download-url

# 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

```python
# -- 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

```python
# -- 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"

```

[Jinja2]: https://github.com/pallets/jinja/


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.
