# Outputfile Extension for the Jinja2 Template Engine
[](https://github.com/jenisys/jinja2-outputfile/actions/workflows/test.yml)
[](https://pypi.python.org/pypi/jinja2-outputfile)
[](https://github.com/jenisys/jinja2-outputfile/blob/main/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
```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.
Raw data
{
"_id": null,
"home_page": "https://github.com/jenisys/jinja2-outputfile",
"name": "jinja2-outputfile",
"maintainer": null,
"docs_url": null,
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7",
"maintainer_email": null,
"keywords": "Jinja2, jinja2, template-extension",
"author": "Jens Engel",
"author_email": "Jens Engel <jenisys@noreply.github.com>",
"download_url": "https://files.pythonhosted.org/packages/ac/a4/baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137/jinja2_outputfile-0.5.1.tar.gz",
"platform": "any",
"description": "# Outputfile Extension for the Jinja2 Template Engine\n\n[](https://github.com/jenisys/jinja2-outputfile/actions/workflows/test.yml)\n[](https://pypi.python.org/pypi/jinja2-outputfile)\n[](https://github.com/jenisys/jinja2-outputfile/blob/main/LICENSE)\n\nProvides a [Jinja2] directive/extension that supports to redirect\nrendered template part(s) to output-file(s).\n\nUSE CASES:\n\n* Redirect a rendered [Jinja2] text template part to an output-file\n* Use a [Jinja2] template as build script\n if many output-files should be generated from the same data model\n\nFEATURES:\n\n* Output suppression: Output files are only written, if the file contents have changed.\n* Verbose mode: Prints which output-files are written and verdict (`CHANGED`, `SAME`).\n* Quiet mode: Verbose mode output is suppressed (use: `jinja_outputfile.QuietOutputFileExtension`)\n\n## EXAMPLE 1: Write rendered text to an output-file\n\n```python\n# -- FILE: example_one.py\n# Redirect rendered text from inside the outputfile block to an output-file.\nfrom jinja2 import Environment\nfrom pathlib import Path\n\ntemplate_as_text = \"\"\"\\\n{% outputfile \"%s/example_%s.txt\"|format(this.output_dir, this.name) -%}\nHello {{this.name}}\n{%- endoutputfile %}\n\"\"\"\n\n# -- EXAMPLE: Using the template\nenv = Environment(extensions=[\"jinja2_outputfile.Extension\"])\ntemplate = env.from_string(template_as_text)\ntemplate.render(this=dict(name=\"Alice\", output_dir=\".\"))\n\n# -- POSTCONDITION: FILE WAS CREATED/WRITTEN (with contents)\noutput_file = Path(\".\")/\"example_Alice.txt\"\noutput_file_contents = output_file.read_text()\nassert output_file.exists()\nassert output_file_contents == \"Hello Alice\\n\"\n```\n\n## EXAMPLE 2: Use multiple output-files\n\n```python\n# -- FILE: example.py\nfrom jinja2 import Environment\nfrom pathlib import Path\n\nTHIS_TEMPLATE = \"\"\"\n{%- for name in this.names -%}\n {% outputfile \"%s/example_%s.txt\"|format(this.output_dir, name) -%}\n Hello {{name}}\n {%- endoutputfile %}\n{% endfor %}\n\"\"\"\n\n# -- EXAMPLE: Using the template\nenv = Environment(extensions=[\"jinja2_outputfile.Extension\"])\ncode_generator = env.from_string(THIS_TEMPLATE)\ncode_generator.render(this=dict(names=[\"Alice\", \"Bob\"], output_dir=\".\"))\n\n# -- POSTCONDITION: FILES were WRITTEN (with contents)\noutput_file1 = Path(\".\")/\"example_Alice.txt\"\noutput_file2 = Path(\".\")/\"example_Bob.txt\"\nassert output_file1.exists()\nassert output_file2.exists()\nassert output_file1.read_text() == \"Hello Alice\\n\"\nassert output_file2.read_text() == \"Hello Bob\\n\"\n\n```\n\n[Jinja2]: https://github.com/pallets/jinja/\n\n\nRationale\n-------------------------------------------------------------------------------\n\nThe `outputfile` directive is useful in a code generator use cases\nif many output files need to be generated from Jinja2 templates.\nIn this case, you can provide one template as control script to accomplish this task.\n\n\nHistory\n-------------------------------------------------------------------------------\n\n* INITIALLY CREATED AS: `simplegen.jinja2_ext.outputfile`\n* REFACTORING: Extracted into own standalone package to simplify reuse\n with [Jinja2] template engine.\n",
"bugtrack_url": null,
"license": null,
"summary": "Jinja2 extension that redirects output to output file(s)",
"version": "0.5.1",
"project_urls": {
"Download": "https://pypi.org/project/jinja2-outputfile",
"Homepage": "https://github.com/jenisys/jinja2-outputfile",
"Issues": "https://github.com/jenisys/jinja2-outputfile/issues/",
"Repository": "https://github.com/jenisys/jinja2-outputfile"
},
"split_keywords": [
"jinja2",
" jinja2",
" template-extension"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b99af050d449623d478725a5989905a46bcd2ca3046e5bbcb923c07714d8217f",
"md5": "289cf09444835ab4eb3468ad87f46b5f",
"sha256": "2e9a969ff5d26a1cb490baa34ae0ca30640e97c0844f45d9e7531226242f6750"
},
"downloads": -1,
"filename": "jinja2_outputfile-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "289cf09444835ab4eb3468ad87f46b5f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7",
"size": 6579,
"upload_time": "2025-08-11T20:45:19",
"upload_time_iso_8601": "2025-08-11T20:45:19.975498Z",
"url": "https://files.pythonhosted.org/packages/b9/9a/f050d449623d478725a5989905a46bcd2ca3046e5bbcb923c07714d8217f/jinja2_outputfile-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "aca4baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137",
"md5": "7662c29ec55fb4e6654bb4083e53f212",
"sha256": "c2bd953a0881ed747aa934a0fc64cfb710f15e12127be7e73a2472172705a4a0"
},
"downloads": -1,
"filename": "jinja2_outputfile-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "7662c29ec55fb4e6654bb4083e53f212",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7",
"size": 13003,
"upload_time": "2025-08-11T20:45:21",
"upload_time_iso_8601": "2025-08-11T20:45:21.555633Z",
"url": "https://files.pythonhosted.org/packages/ac/a4/baa0e797f6a719cddebcf4fe3f3588c0f3bbf8a61a75ed9b90e63fc94137/jinja2_outputfile-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-11 20:45:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jenisys",
"github_project": "jinja2-outputfile",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "jinja2-outputfile"
}