Closed nicktids closed 1 year ago
We recently changed the behavior for FastAPI to return a Response object instead of pure text, but I never released a packaged version with that change. I just did that and pushed it to PyPi as 1.1.0. Could you download the new version and check if that works as you would expect, please?
Hi!
Then comes out perfect
Can you show the code for it, please?
No matter what I do, I get these extra newlines and this behavior is expected / on par with the default Jinja2 environment. The FastAPI (Starlette) templating works similarly.
Code
from fastapi import FastAPI
from fastapi.requests import Request
from fastapi.templating import Jinja2Templates
from jinja2_fragments.fastapi import Jinja2Blocks
app = FastAPI()
templ_default = Jinja2Templates(directory="templates")
templ_blocks = Jinja2Blocks(directory="templates")
@app.get("/blocks/full_page")
async def full_page_blocks(request: Request):
return templ_blocks.TemplateResponse(
"items.html.jinja",
{"request": request, "items": range(5)},
)
@app.get("/blocks/items")
async def only_content_blocks(request: Request):
return templ_blocks.TemplateResponse(
"items.html.jinja",
{"request": request, "items": range(5)},
block_name="items",
)
@app.get("/default/full_page")
async def full_page_default(request: Request):
return templ_default.TemplateResponse(
"items.html.jinja",
{"request": request, "items": range(5)},
)
Tempaltes
```html
```html {% extends "base.html.jinja" %} {% block items %}
Rendered HTML
```
```
```
If you don't want that extra whitespace, you may want to check this.
Here's how it looks:
templ_blocks = Jinja2Blocks(
directory="templates",
trim_blocks=True,
)
```
There's also lstrip_block=True
which removes even more space.
Closing as @senpos provided an explanation of the behavior, and it's consistent with default jinja output.
Using fastapi[all]==0.103.1 jinja2-fragments==1.0.0
I'm getting lots of \n new lines all over the block when using the Jinja2Blocks
Output and it is stringified with quotations
but if I put this in a partial html file with just
Then comes out perfect
Can you understand why it might be creating extra line items.
Using python3.11-bullseye docker container