djlint / djLint

✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang
https://djLint.com
GNU General Public License v3.0
664 stars 81 forks source link

[BUG] [Formatter] Conditional HTML enclosing tags leads to added indentation #840

Open PetrDlouhy opened 2 months ago

PetrDlouhy commented 2 months ago

System Info

Issue

If I want to add/not add enclosing HTML tag in my template I do it like this:

{% if enclose_in_dif %}
    <div>
{% endif %}

<h1>Test</h1>

{% if enclose_in_dif %}
    </div>
{% endif %}

When I run djlint --reformat, I got this result:

{% if enclose_in_dif %}<div>{% endif %}
    <h1>Test</h1>
    {% if enclose_in_dif %}</div>{% endif %}

Which is the worst possibility what could have happened. It will introduce new indentation in the document.

My pyproject.toml:

  [tool.djlint]
  profile="django"
  custom_blocks="cache,scribble,experiment,recursetree,linebreakless,blocktrans,html2text"
  custom_html="source,picture,img,defs,symbol,path"