Use the declarative PartialsJinjaExtension
mainYou can use PartialsJinjaExtension for a declarative approach. This is useful for frameworks that configure Jinja2 environments declaratively or when using standalone Jinja2 environments.
Important Note for Flask users:
When using the extension approach, partials render directly through the Jinja environment rather than flask.render_template. Consequently, Flask context processors (@app.context_processor) and the template_rendered signal do not apply inside partials. If your partials depend on these, use the traditional register_extensions(app) method instead.
# For Flask
app.jinja_env.add_extension('jinja_partials.PartialsJinjaExtension')
# For Standalone Jinja2
from jinja2 import Environment, FileSystemLoader
environment = Environment(
loader=FileSystemLoader("templates"),
extensions=["jinja_partials.PartialsJinjaExtension"]
)