-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass endpoint dependencies to Jinja context #4
Comments
I'm not sure it can be guaranteed that the mapping returned by the route will always be mutable. If you need the route's dependencies in addition to its return value for rendering, and you know that the return value is mutable, then I think the best solution (at least for now) is to subclass |
That's a problem 😞
Well, I'm not actively developing applications with HTMX or Jinja2 for now. So it's not a real deal for me but it probably (most likely) will one day and that day I can subclass this one but it'll be probably a common issue. I think we should think about a solution. What should we do? Try to find a solution, close this, or keep it open to prevent anyone else from opening a similar issue. |
One idea... def render(result: Any, *, context: dict[str, Any], request: Request) -> HTMLResponse:
request.state.depends = context
return self.templates.TemplateResponse(name=template_name, request=request, context=result) This way, we can access the dependencies from request.state.depends and we can set it to a variable inside jinja template to access it easily. Info: |
I think the best solution is to add another class JinjaContextFactory(Protocol):
def __call__(self, result: Any, route_context: dict[str, Any]) -> dict[str, Any]:
... Additionally, the
This way users could configure their rendering context creation however they need or want to. |
This looks good. |
It should be fixed by #11 |
Well I didn't checked that but closing this, great work 🚀. |
We can not access endpoint dependencies from Jinja templates as of now. What if we change this block:
fasthx/fasthx/main.py
Lines 141 to 142 in 2003d8c
as this:
Maybe another name for the key,
private
instead ofdepends
?The text was updated successfully, but these errors were encountered: