sponsfreixes / jinja2-fragments

Render Jinja2 template block as HTML page fragments on Python web frameworks.
MIT License
245 stars 13 forks source link

Getting New Lines #18

Closed nicktids closed 1 year ago

nicktids commented 1 year ago

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

{% block test_items %}
        <div id="items">

            {% for item in items %}
                <li>{{ item }}</li>
            {% endfor %}
        </div>
{% endblock %}

Output and it is stringified with quotations

"\n
\n
\n\n \n
 Item1
\n \n
 Item2
\n \n
\n
\n"

but if I put this in a partial html file with just

<ul>
    {% for item in items %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>

Then comes out perfect

Can you understand why it might be creating extra line items.

Using python3.11-bullseye docker container

sponsfreixes commented 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?

senpos commented 1 year ago

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

base.html.jinja

```html This is the title

This is a header

{% block items %} {% endblock %} ```

items.html.jinja

```html {% extends "base.html.jinja" %} {% block items %}

    {% for item in items %}
  • {{ item }}
  • {% endfor %}
{% endblock %} ```

Rendered HTML

blocks_full_page.html

``` This is the title

This is a header

  • 0
  • 1
  • 2
  • 3
  • 4
```

blocks_items.html

```

  • 0
  • 1
  • 2
  • 3
  • 4
```

default_full_page.html

``` This is the title

This is a header

  • 0
  • 1
  • 2
  • 3
  • 4
```

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,
)
blocks_full_page.html (trim)

``` This is the title

This is a header

  • 0
  • 1
  • 2
  • 3
  • 4
```

There's also lstrip_block=True which removes even more space.

sponsfreixes commented 1 year ago

Closing as @senpos provided an explanation of the behavior, and it's consistent with default jinja output.