djlint / djLint

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

[BUG] [Formatter] Incorrect indentation of custom Django template tags with children values #472

Closed janbaykara closed 1 year ago

janbaykara commented 1 year ago

System Info

Issue

Custom template tags provided by slippers are incorrectly indented.

When I write code like this:

    {% fragment as label %}
       {% for category in page.category.all %}<span>{{ category }}</span>{% endfor %}
    {% endfragment %}

djlint auto-formats it to this:

    {% fragment as label %}
    {% for category in page.category.all %}<span>{{ category }}</span>{% endfor %}
{% endfragment %}

How To Reproduce

  1. Install slippers
  2. Use the fragment code as above
  3. Run formatting via djlint
  4. See that the indenting is off
christopherpickering commented 1 year ago

Any chance you can share your config? Have you added fragment as a custom block?

janbaykara commented 1 year ago

@christopherpickering fragment is a template tag provided by Slippers. Implementation of the template tag is here, for interest.

Here's the relevant config section from pyproject.toml:

[tool.djlint]
profile = "django"
use_gitignore = true
ignore="H006,H031" # Ignore img width/height requirements
christopherpickering commented 1 year ago

thanks!

the fragment tag should be in the config:

[tool.djlint]
profile = "django"
use_gitignore = true
ignore="H006,H031" # Ignore img width/height requirements
custom_blocks="fragment"

image

Does that work for you? Thanks for linking the slippers docs, we can probably include thier tags in the default set.

janbaykara commented 1 year ago

I see, I didn't realise one had to specify tags for them to be formatted... I assume this won't be necessary after the reimplementation of DJLint with an AST?

christopherpickering commented 1 year ago

The tags included with django/jinja/twig/etc are formated by default but custom tags need to be specified right now. We will see what happens in the next edition... still working on that code.

janbaykara commented 1 year ago

I feel like, for a Django project, given that all of my HTML will be interpreted through the Django template engine, I'd expect all {% ... %} ... {% ... %} syntax to be nested without exception.

I wonder if this warrants a general flag being added to the config, if you are running a Django project? Potentially, alternatively, a file-level comment flag.

christopherpickering commented 1 year ago

well, there are also {% %} tags that are not indented, for example the include and load tags... but I supposed if there is an {% end... trailing it, then the contents could be indented.

janbaykara commented 1 year ago

That makes sense!

christopherpickering commented 1 year ago

:tada: This issue has been resolved in version 1.19.17 :tada:

The release is available on:

Your semantic-release bot :package::rocket: