Skip to content
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

[Formatter] jinja whitespace control and formatter inline whitespace removal #841

Open
3 tasks done
anentropic opened this issue Jun 18, 2024 · 0 comments
Open
3 tasks done
Labels
🦠 bug Something isn't working 🧽 formatter

Comments

@anentropic
Copy link

System Info

  • OS: macos
  • Python Version (3.11.5)
  • djLint Version (1.34.1)
  • template language: e.g. jinja2

Issue

I have some jinja that is formatted like this after formatting:

                <tr {% if stat.css_classes %}
                    class="{{ stat.css_classes }}"
                    {% endif %}>

I am trying to use jinja whitespace control to end up with rendered html like:

                <tr class="text-warning">

I've enabled trim_blocks and lstrip_blocks

I can do this:

                <tr {% if stat.css_classes -%}
                    class="{{ stat.css_classes }}"
                    {%- endif %}>

and get:

                <tr class="text-warning">

But in the case where the conditional is false I still have an extra whitespace:

                <tr >

I could do this:

                <tr {%- if stat.css_classes %} class="{{ stat.css_classes }}"{% endif %}>

and all the cases would look right when rendered

But the formatter wants to get rid of the inner whitespace here %} class

...it's this behaviour I'd like to have control over - maybe an option to preserve such whitespace?

Other option that works right now is:

                <tr {%- if stat.css_classes -%}
                    {{ ' ' }}class="{{ stat.css_classes }}"
                    {%- endif %}>

{{ ' ' }} makes a whitespace that resists being collapsed by either jinja or the formatter, which allows to contract the regular whitespace aggressively on jinja side

construct is a bit ugly but maybe this is best option after all

however the formatter now wants to put that on one line:

                <tr {%- if stat.css_classes -%}{{ ' ' }}class="{{ stat.css_classes }}"{%- endif %}>

that's ok for me, but if I can't have multi-line formatting I'd rather be able to have a normal whitespace

How To Reproduce

see above

@anentropic anentropic added 🦠 bug Something isn't working 🧽 formatter labels Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🦠 bug Something isn't working 🧽 formatter
Projects
None yet
Development

No branches or pull requests

1 participant