{"id":4585,"library":"jinja-partials","title":"Jinja Partials","description":"Jinja Partials is a Python library designed for the simple reuse of partial HTML page templates within the Jinja template language, particularly for Python web frameworks. It addresses the 'long template problem' by enabling function-like reusability for HTML fragments. The library is currently active, with its latest version being 0.3.1, and shows a consistent release cadence.","status":"active","version":"0.3.1","language":"en","source_language":"en","source_url":"https://github.com/mikeckennedy/jinja_partials","tags":["jinja2","html","templates","web","flask","starlette","fastapi","quart","reusability","partials"],"install":[{"cmd":"pip install jinja-partials","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"For Flask applications.","symbol":"register_extensions","correct":"from jinja_partials import register_extensions"},{"note":"For FastAPI applications.","symbol":"register_fastapi_extensions","correct":"from jinja_partials import register_fastapi_extensions"},{"note":"For Starlette applications.","symbol":"register_starlette_extensions","correct":"from jinja_partials import register_starlette_extensions"},{"note":"For Quart applications.","symbol":"register_quart_extensions","correct":"from jinja_partials import register_quart_extensions"},{"note":"For standalone Jinja2 environments outside of a web framework.","symbol":"register_environment","correct":"from jinja_partials import register_environment"},{"note":"For declarative registration as a Jinja2 extension (available since v0.3.0).","symbol":"PartialsJinjaExtension","correct":"from jinja_partials import PartialsJinjaExtension"}],"quickstart":{"code":"import flask\nfrom jinja_partials import register_extensions\nimport os\n\napp = flask.Flask(__name__)\n\n# Assuming templates are in a 'templates' directory and partials in 'templates/partials'\nregister_extensions(app)\n\n@app.route('/')\ndef index():\n    return flask.render_template('index.html', title='Home Page', user={'name': 'Alice'})\n\n# Example template structure:\n# templates/\n#   index.html\n#   partials/\n#     greeting.html\n\n# index.html content:\n# <!DOCTYPE html>\n# <html>\n# <head><title>{{ title }}</title></head>\n# <body>\n#   <h1>Welcome!</h1>\n#   {{ render_partial('greeting.html', name=user.name) }}\n# </body>\n# </html>\n\n# partials/greeting.html content:\n# <p>Hello, {{ name }} from a partial!</p>\n\nif __name__ == '__main__':\n    # In a real app, you'd run with `flask run` or a WSGI server.\n    # For this quickstart, we'll simulate rendering directly.\n    # You would typically run `app.run(debug=True)`\n    print(\"Quickstart setup complete. Run this with a Flask development server.\")","lang":"python","description":"This quickstart demonstrates how to integrate `jinja-partials` with a Flask application. It shows how to register the extension and use the `render_partial` function in your templates. Create an `index.html` and a `partials/greeting.html` inside a `templates` directory, then run the Flask app."},"warnings":[{"fix":"Ensure your project is running on Python 3.10 or newer. Upgrade your Python environment if necessary.","message":"The minimum required Python version for `jinja-partials` is now 3.10.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Upgrade to `jinja-partials` version 0.3.1 or newer to resolve issues with asynchronous rendering in async web frameworks.","message":"Async rendering issues, specifically `asyncio.run() cannot be called from a running event loop`, were fixed in v0.3.1 for async frameworks (Quart, FastAPI, Starlette).","severity":"gotcha","affected_versions":"<0.3.1"},{"fix":"Review calls to `register_starlette_extensions`. The `app` parameter can now be omitted if not needed for lifecycle management.","message":"The `app` parameter for `register_starlette_extensions` became optional in v0.3.1. While often passed, its previous mandatory status might cause minor type-checking or linting issues if not updated.","severity":"gotcha","affected_versions":">=0.3.1"},{"fix":"Understand that `render_partial()` offers parameter passing and local scope, unlike `{% include '...' %}`. Use `jinja-partials` when you need to pass data specifically to a reusable fragment like a function.","message":"Jinja's native `include` directive differs fundamentally from `jinja-partials`. `include` acts like a C++ preprocessor macro without parameters, merely inserting template content. `jinja-partials` provides function-like reusability with explicit parameters and local variable scope, offering a more robust way to manage reusable HTML fragments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider using `app.jinja_env.add_extension('jinja_partials.PartialsJinjaExtension')` (for Flask) or adding `\"jinja_partials.PartialsJinjaExtension\"` to your `extensions` list for standalone Jinja2 environments, especially if you prefer a declarative setup.","message":"A new declarative approach using `PartialsJinjaExtension` was introduced in v0.3.0. This offers an alternative to the `register_..._extensions` functions, particularly useful for declarative Jinja2 environment configurations.","severity":"gotcha","affected_versions":">=0.3.0"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}