Skip to content

Improve formatting of Jinja blocks#205

Draft
UnknownPlatypus wants to merge 5 commits intog-plane:mainfrom
UnknownPlatypus:fix-wrapping-if-block
Draft

Improve formatting of Jinja blocks#205
UnknownPlatypus wants to merge 5 commits intog-plane:mainfrom
UnknownPlatypus:fix-wrapping-if-block

Conversation

@UnknownPlatypus
Copy link
Copy Markdown
Contributor

@UnknownPlatypus UnknownPlatypus commented Feb 28, 2026

Given this:

<p>
{% if alert_level == "low" %} the sky condition{% elif alert_level == "storm" %} the Storm warning{% else %} the Heatwave alert{% endif %}
</p>

It was previously getting formatted to this:

<p>
    {% if alert_level == "low" %}
        the sky condition{% elif alert_level == "storm" %}
        the Storm warning{% else %}
        the Heatwave alert{% endif %}
</p>

And will now respect the whitespace sensitivity of the parent node (the p tag here) and be formatted like so:

<p>
    {% if alert_level == "low" %}
        the sky condition
    {% elif alert_level == "storm" %}
        the Storm warning
    {% else %}
        the Heatwave alert
    {% endif %}
</p>

I think it make sense because jinja block/tags basically disapear when rendered.

Test cases come from real world exemple were the formatting was weird previously.
You can see the before in this commit, and the after in the last commit

@netlify
Copy link
Copy Markdown

netlify Bot commented Feb 28, 2026

Deploy Preview for markup-fmt ready!

Name Link
🔨 Latest commit 20af8b3
🔍 Latest deploy log https://app.netlify.com/projects/markup-fmt/deploys/69c300488eee30000825339d
😎 Deploy Preview https://deploy-preview-205--markup-fmt.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@UnknownPlatypus UnknownPlatypus changed the title Improve formatting of django blocks Improve formatting of Jinja blocks Mar 1, 2026
@UnknownPlatypus UnknownPlatypus marked this pull request as draft March 1, 2026 22:48
@UnknownPlatypus UnknownPlatypus force-pushed the fix-wrapping-if-block branch 2 times, most recently from 3518f4b to 1c0051f Compare March 24, 2026 21:14
@UnknownPlatypus
Copy link
Copy Markdown
Contributor Author

@g-plane I found an interesting case where this is unsafe to apply the current changes, jinja trans block

-            {% trans %}Your server has exceeded the limit for how often this action can be performed.{% endtrans %}
+            {% trans %}
+                Your server has exceeded the limit for how often this action can be performed.
+            {% endtrans %}

How can we manage to have the information about the parent jinja tag inside format_control_structure_block_children, it's seems we only know about the parent html node ?

Finding other edge cases that make me think this is much more involved than I thought. For exemple this change would be incorrect too I think:

             _('Organization URL')
         }}</label>
         <div id="id_realm_subdomain" class="not-editable-realm-field">
-            {% if form.realm_subdomain.value() %}{{ form.realm_subdomain.value() }}.{%
-                endif
-            %}{{ external_host }}
+            {% if form.realm_subdomain.value() %}
+                {{ form.realm_subdomain.value() }}.
+            {% endif %}{{ external_host }}
         </div>
     </div>
 </div>

@g-plane
Copy link
Copy Markdown
Owner

g-plane commented Mar 25, 2026

We can limit the behavior of inserting line breaks to a set of known tags, but don't do this check in format_control_structure_block_children.

@UnknownPlatypus
Copy link
Copy Markdown
Contributor Author

but don't do this check in format_control_structure_block_children.

We should do it in the doc implementation of JinjaTag then ?

@g-plane
Copy link
Copy Markdown
Owner

g-plane commented Mar 26, 2026

Maybe in JinjaBlock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants