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

[BUG] [Formatter] Formatting breaks if/else chains in html attributes #195

Closed
3 tasks done
kageurufu opened this issue Mar 9, 2022 · 5 comments
Closed
3 tasks done

Comments

@kageurufu
Copy link
Contributor

System Info

  • OS: Arch Linux on WSL2
  • Python Version: 3.10.2
  • djLint Version: 0.7.5
  • template language: jinja2

Issue

The formatted is breaking attributes containing templates including more double quotes.

Sample HTML to reproduce is

<a target="_blank" href="
    {%- if entry["type"] == "Bank Deposit" -%}
        {{ url_for("accounting.receivable.bank_deposit.bank_deposit", bank_deposit_id=entry["id"]) }}
    {%- elif entry["type"] == "Journal Entry Line" -%}
        {{ url_for("accounting.journal_entries.journal_entry", journal_entry_id=entry["id"]) }}
    {%- elif entry["type"] == "Payment" -%}
        {{ url_for("invoice.view_payment", payment_id=entry["id"]) }}
    {%- elif entry["type"] == "Payment Transaction" -%}
        {{ url_for("invoice.transaction.transaction", id=entry["id"]) }}
    {%- elif entry["type"] == "Receipt" -%}
        {{ url_for("accounting.receivable.receipt.receipt", receipt_id=entry["id"]) }}
    {%- endif -%}
    ">

The result I get is

<a target="_blank"
   href=" {%- if entry["type"] == "Bank Deposit" -%} {{ url_for("
     accounting
     receivable
     bank_deposit
     bank_deposit
     bank_deposit_id
     entry
     id
   {%- elif entry["type"] == "Journal Entry Line" -%}
     {{ url_for("accounting.journal_entries.journal_entry", journal_entry_id=entry["id"]) }}
   {%- elif entry["type"] == "Payment" -%}
     {{ url_for("invoice.view_payment", payment_id=entry["id"]) }}
   {%- elif entry["type"] == "Payment Transaction" -%}
     {{ url_for("invoice.transaction.transaction", id=entry["id"]) }}
   {%- elif entry["type"] == "Receipt" -%}
     {{ url_for("accounting.receivable.receipt.receipt", receipt_id=entry["id"]) }}
   {%- endif -%}>

Additionally, when formatting the following with or without format_attribute_template_tags enabled, the reformatting will collapse the template into one line

<a target="_blank" href="
    {%- if entry['type'] == 'Bank Deposit' -%}
        {{ url_for('accounting.receivable.bank_deposit.bank_deposit', bank_deposit_id=entry['id']) }}
    {%- elif entry['type'] == 'Journal Entry Line' -%}
        {{ url_for('accounting.journal_entries.journal_entry', journal_entry_id=entry['id']) }}
    {%- elif entry['type'] == 'Payment' -%}
        {{ url_for('invoice.view_payment', payment_id=entry['id']) }}
    {%- elif entry['type'] == 'Payment Transaction' -%}
        {{ url_for('invoice.transaction.transaction', id=entry['id']) }}
    {%- elif entry['type'] == 'Receipt' -%}
        {{ url_for('accounting.receivable.receipt.receipt', receipt_id=entry['id']) }}
    {%- endif -%}
    ">

How To Reproduce

format either provided template

Our settings

[tool.djlint]
indent = 2
profile = "jinja"
max_line_length = 120
blank_line_after_tag = "from,import,endblock"
custom_blocks = "debug,permission"
ignore = "H017,T003"
# format_attribute_template_tags = true
@kageurufu kageurufu added 🦠 bug Something isn't working 🧽 formatter labels Mar 9, 2022
@kageurufu kageurufu changed the title [BUG] [Formatter] [BUG] [Formatter] Formatting breaks if/else chains in html attributes Mar 10, 2022
@PetrDlouhy
Copy link

I have similar problem with Django templates:

-                <img
-                    {% if value.image.file.url|split:"."|last|lower == "gif" %}
-                        src="{{value.image.file.url}}"
-                    {% else %}
-                        {% image value.image fill-640x360   as block_image_640 %}
-                        {% image value.image fill-768x432   as block_image_768 %}
-                        {% image value.image fill-1024x576  as block_image_1024 %}
-                        {% image value.image fill-1600x900  as block_image_1600 %}
-                        data-src="{{ block_image_640.url }}"
-                        data-srcset="
-                            {{ block_image_640.url }} 640w,
-                            {{ block_image_768.url }} 768w,
-                            {{ block_image_1024.url }} 1024w,
-                            {{ block_image_1600.url }} 1600w
-                        "
-                        sizes="(min-width: 1200px) 458px, (min-width: 992px) 374px, (min-width: 768px) 720px, calc(100vw - 30px)"
-                    {% endif %}
-                    class="richtext-image imageblock overflow {{ value.image_position }} lazy"
-                    title="{{ value.image.title }}"
-                    alt="Block image"
-                />
+                <img {% if value.image.file.url|split:"."|last|lower == "gif" %} src="{{ value.image.file.url }}" {% else %} {% image value.image fill-640x360   as block_image_640 %} {% image value.image fill-768x432   as block_image_768 %} {% image value.image fill-1024x576  as block_image_1024 %} {% image value.image fill-1600x900  as block_image_1600 %} data-src="{{ block_image_640.url }}" data-srcset=" {{ block_image_640.url }} 640w, {{ block_image_768.url }} 768w, {{ block_image_1024.url }} 1024w, {{ block_image_1600.url }} 1600w " sizes="(min-width: 1200px) 458px, (min-width: 992px) 374px, (min-width: 768px) 720px, calc(100vw - 30px)" {% endif %}
+                     class="richtext-image imageblock overflow {{ value.image_position }} lazy"
+                     title="{{ value.image.title }}"
+                     alt="Block image"/>

I am not sure why it does that, while on most other places it splits the long lines rather nicely.

@Kakadus
Copy link

Kakadus commented Sep 5, 2022

I am experiencing this bug as well and were able to construct a minimal example.
With

<a {% if cond %} class="somelongclass {% if cond2 %}otherlongclass{% endif %} anotherlongclass"{% endif %}></a>

the nested ifs break the code:

<a {% if cond %} class="somelongclass {% if cond2 %}otherlongclass{% endif %}
    anotherlongclass
{% endif %}></a>

Configuration with pyproject.toml:

[tool.djlint]
profile = "django"
format_attribute_template_tags = false

With format_attribute_template_tags = true, everything is reformatted as

<a {% if cond %}
       class="somelongclass
              {% if cond2 %}
                  otherlongclass
              {% endif %}
              anotherlongclass
          {% endif %}></a>

@christopherpickering
Copy link
Contributor

Thanks, I'm putting a release that updates how attributes are parsed and will fix this issue.

However, in this update, we will no longer be splitting href's into multiple lines per #427, but if you were have syntax like this in other attributes it will be fixed.

@Kakadus thanks, the nested if statements is a separate problem, I'll open a new issue for that.

@christopherpickering
Copy link
Contributor

@PetrDlouhy Thanks!

I will open a separate issue for this.
When using the format_attribute_template_tags = true the {% image %} blocks should be on their own line.

christopherpickering pushed a commit that referenced this issue Nov 3, 2022
## [1.19.4](v1.19.3...v1.19.4) (2022-11-03)

### Bug Fixes

* **attributes:** fixed poor django attribute parsing ([bd90f08](bd90f08)), closes [#427](#427) [#438](#438)
* **attributes:** prevented attribute indentation for url type attributes ([907a1bf](907a1bf)), closes [#320](#320) [#86](#86) [#195](#195)
* **image tag:** added image tag to template break tag list. srcset formatting ([0d170aa](0d170aa)), closes [#444](#444)
@christopherpickering
Copy link
Contributor

🎉 This issue has been resolved in version 1.19.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

4 participants