Replies: 5 comments 3 replies
-
Layout.jinja: router: |
Beta Was this translation helpful? Give feedback.
-
you have to pass the request as part of the key-value pairs in the context for Jinja2. So, you also have to declare it in your path operation. |
Beta Was this translation helpful? Give feedback.
-
So this took me a little bit of time to get correct since I wanted to bind to the existing starlette Here are some useful code snippets
Index.jinja
SampleComponent.jinja
|
Beta Was this translation helpful? Give feedback.
-
@thornewolf, this helped me loads. Maybe worth adding to the documentation. thank you for posting it here. is there a way to avoid having to pass requests all the time I am using |
Beta Was this translation helpful? Give feedback.
-
A simpler way IMO is to add the extension import jinjax
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# add this
templates.env.add_extension(jinjax.JinjaX)
catalog = jinjax.Catalog(jinja_env=templates.env)
catalog.add_folder("templates/components")
@app.get("/")
async def read_root(request: Request):
return templates.TemplateResponse("index.html", {"request": request}) |
Beta Was this translation helpful? Give feedback.
-
@router.get('/article', summary='Article信息') async def get_article(id: int, db: AsyncSession = Depends(get_db)):
Jinjax will blocked when use async of fastapi?
Beta Was this translation helpful? Give feedback.
All reactions