Skip to content

Conversation

@pydanny
Copy link
Member

@pydanny pydanny commented Aug 3, 2025

Solution ideas

  • Wrap the result of the JinjaRenderer
  • Add a new FastAPI encoder for Jinja objs

Tasks remaining to close

  • Add test
  • Fix bug
  • Provide documentation using JinjaRenderer within Air Tags

@pydanny pydanny added the bug label Aug 3, 2025
@pydanny pydanny linked an issue Aug 3, 2025 that may be closed by this pull request
@pydanny pydanny added help wanted python Pull requests that update python code Air Tags labels Aug 3, 2025
@pydanny pydanny force-pushed the 231-jinja-renderer-not-rendering-from-within-air-tags branch from 6071ba4 to d76565d Compare August 3, 2025 07:49
@pydanny
Copy link
Member Author

pydanny commented Aug 3, 2025

We're running into a bit of chicken-vs-egg, in that:

  1. The jinja function can't know if it is called from within a Tag or as a TemplateResponse during ASGI evaluation
  2. If called within a tag it needs to return a String or SafeStr. If called as a Response it needs to return a Response.
  3. Error handling this deep into things appears rigid, not something easily manoeuvred around or monkeypatched. In theory we can inspect the outer frame via the inspect module, but this is slow and problematic
  4. Our planned solution is to have a string-like response that behaves like a string yet has the same attributes as a Starlette response.

@pydanny
Copy link
Member Author

pydanny commented Aug 16, 2025

Gave this another go. Another problem is that the str type is quite far in design from the starlette.response.Response type. Either that or my design is off.

I could solve this with frame inspection but that's quite slow and probably brittle in a high load environment.

Quite frustrating as this is what I consider a crucial feature for the project.

What if we have the jinja callable produced by the JinjaRenderer to have a base argument so it's called like this:

import air

app = air.Air()
jinja = air.JinjaRenderer('templates')

@app.page
def index():
    return jinja(request, 'home.html', base=True,
        air.Article(
            jinja(request, 'article.html', content='TEXT HERE'
        )
    )

Alternatively or in addition to base we could have a fragment argument:

import air

app = air.Air()
jinja = air.JinjaRenderer('templates')

@app.page
def index():
    return jinja(request, 'home.html',
        air.Article(
            jinja(request, 'article.html', fragment=True, content='TEXT HERE'
        )
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jinja renderer not rendering from within Air Tags

2 participants