djLint icon indicating copy to clipboard operation
djLint copied to clipboard

[BUG] [Formatter] Nested if statements in attribute values

Open christopherpickering opened this issue 2 years ago • 1 comments

from @Kakadus in #195 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 avatar Nov 03 '22 10:11 christopherpickering

I can't come up with a regex to match nested statements properly... I thinking this will need to wait until we have a decent attribute parser working.

christopherpickering avatar Nov 03 '22 11:11 christopherpickering