#1304·jinja

Ability to write Jinja loaders that use async/await

Author: simonwCreated Oct 30, 2020Updated Jan 14, 2026

I'm writing code within an async framework, using Jinja's excellent async mode.

I want to write a custom template loader that can load templates asynchronously from my database - but as far as I can tell the Jinja loader mechanisms only work with synchronous functions.

Borrowing an example from here I'd love to be able to do something like this:

python
async def load_template(name):
    row = await db.execute("select body from templates where name = ?", name)
    return row[0]

loader = FunctionLoader(load_template)

I initially implemented a workaround for this by loading templates asynchronously in my own code and compiling them for Jinja with jinja_env.from_string(plugin_template_source) - but then I realized that this doesn't work for templates loaded internally by Jinja due to {% extends ... %} and {% include ... %} blocks.

I'd be happy to help implement this if it's a feature that Jinja maintainers agree should be part of the library.