dave2wave commented on issue #372:
URL:
https://github.com/apache/tooling-trusted-releases/issues/372#issuecomment-3621053011
This can be done, but changes are needed to Quart configuration. Probably we
need to contribute to `apache/infrastructure-asfquart`
Ahh—GOT IT! And this is **such an important detail in Quart/Jinja setups!**
If `{%- … -%}` *isn’t stripping whitespace*, it’s almost always because the
**Jinja environment isn’t configured to allow it**, even though the syntax is
valid.
And the great news is: **Quart does NOT enable whitespace stripping by
default.**
So `{%-` behaves exactly like `{%` unless you turn on two specific
environment options.
Let’s fix that!
---
# ✅ **1. Enable whitespace trimming in Quart**
You must set **both** of these on the Jinja environment:
* `trim_blocks = True`
* `lstrip_blocks = True`
In Quart:
```python
from quart import Quart
app = Quart(__name__)
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
```
🔥 After this, `{%-` and `-%}` will finally work exactly as expected.
---
# ⭐ Why both are required
| Setting | Effect |
| -------------------- | ----------------------------------------------- |
| `trim_blocks=True` | Strips the newline *after* a Jinja block (`%}`) |
| `lstrip_blocks=True` | Strips whitespace *before* a Jinja block (`{%`) |
Whitespace control operators `{%-`, `-%}`, `{{-`, `-}}` only fully work when
these are enabled.
Without these settings, **Jinja leaves newlines intact**, even when writing
`{%-`.
---
# 🎉 Quick proof
Template:
```jinja2
<title>{%- block title -%}{%- endblock -%}</title>
```
Without settings → output contains newlines around the block
With settings → output collapses to:
```html
<title>My Title</title>
```
---
# ⚡ Extra option: auto-escape consistency
While you're tuning settings, adding this helps too:
```python
app.jinja_env.autoescape = True
```
(Quart defaults this, but if you're customizing envs, check it.)
---
# If you'd like, I can help you confirm your environment settings or adjust
them to your API layout.
Just show me your Quart setup — I LOVE helping get these template engines
razor-clean!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]