djlint / djLint

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

[BUG] [Formatter] object/array variable whitespace is removed #370

Closed tomdavies closed 1 year ago

tomdavies commented 2 years ago

System Info

Issue

All whitespace is removed from multi-line {% set = {} %} statements (e.g when declaring a nested object), and also from object function parameters formatted in the same way.

How To Reproduce

Given the input:

{% set foo = {
  bar: {
    baz: {
      cux: 1
    }
  }
} %}

{% set fruit = [
  'Apples', 
  'Pears',
  'Bananas'
] %}

{{ myfunc({
  bar: {
    baz: {
      cux: 1
    }
  }
})

I get the output

{% set foo = {
bar: {
baz: {
cux: 1
}
}
} %}
{% set fruit = [
'Apples',
'Pears',
'Bananas'
] %}
{{ myfunc({
bar: {
baz: {
cux: 1
}
}
})

When I expect indentation to be applied to the keys/values in my variable declarations per my .djlintrc:

{
  "extension": "twig",
  "profile": "nunjucks"

}

Thanks so much for your work on djLint!

christopherpickering commented 2 years ago

similar case as #287

oliver-schulz commented 1 year ago

Hello we have the same problem here when using djlint with the same configuration. Is there a way to exclude theses from linting through the config file?

Before:

<div class="w-full">
  {{
    sylius_template_event(
      'sylius.shop.cart.summary',
      {
        cart: cart
      }
    )
  }}
</div>

After:

<div class="w-full">
    {{
    sylius_template_event(
    'sylius.shop.cart.summary',
    {
    cart: cart
    }
    )
    }}
</div>
christopherpickering commented 1 year ago

@oliver-schulz sorry I missed the question about excluding. If you didn't already find it, you can do it like:

<div class="w-full">
    {# djlint:off #}
    {{
    sylius_template_event(
        'sylius.shop.cart.summary',
        {
        cart: cart
        }
    )
    }}
    {# djlint:on #}
</div>
christopherpickering commented 1 year ago

Link to twig expressions: https://twig.symfony.com/doc/3.x/templates.html#expressions

christopherpickering commented 1 year ago

@tomdavies @oliver-schulz thanks for waiting on this, the next release will fix both examples you provide :)

Just noting in @tomdavies there was a typo, the closing }} were missing. I assumed it was an accident and made djlint format only full tags ;)

tomdavies commented 1 year ago

Thanks @christopherpickering much appreciated! Will check this out. And yes, that was a typo in my crumby example code 😄

christopherpickering commented 1 year ago

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

The release is available on:

Your semantic-release bot :package::rocket: