code-django icon indicating copy to clipboard operation
code-django copied to clipboard

New lines get removed when saving HTML files

Open IArnaut2 opened this issue 1 year ago • 2 comments

I use Prettier. I've tried Django extension, Better Jinja and Django Support in VS Code, new lines get removed on save for some reason. This code:

{% with available=terms.available|date:"Y-m-d" created_at=listing.created_at|date:"Y-m-d" %}
    {% if terms.available == listing.created_at %}
    Odmah
    {% else %}
    {{terms.available | date:"d.m.Y"}}
    {% endif %}
{% endwith %}

turns into this on save:

{% with available=terms.available|date:"Y-m-d"
created_at=listing.created_at|date:"Y-m-d" %} {% if terms.available ==
listing.created_at %} Odmah {% else %} {{terms.available |
date:"d.m.Y"}} {% endif %} {% endwith %}

which also causes TemplateSyntaxError. How do I keep new lines intact?

IArnaut2 avatar Apr 11 '25 07:04 IArnaut2

Try adding this to the settings

"html.format.templating": true,

almahdi404 avatar Apr 11 '25 07:04 almahdi404

If you are using Prettier, https://github.com/junstyle/prettier-plugin-django might solve this.

almahdi404 avatar Apr 11 '25 07:04 almahdi404

How do I use the extension with djlint?

EDIT: I did this:

"[django-html][html]": {
    "editor.defaultFormatter": "monosans.djlint"
}

IArnaut2 avatar May 03 '25 22:05 IArnaut2

That should work. Did it?

And you don't even need [django-html]

almahdi404 avatar May 04 '25 08:05 almahdi404

That should work. Did it?

And you don't even need [django-html]

It worked. I had to make djlint be a formatter for Django-Html, because prettier doesn't work well with template engines.

IArnaut2 avatar May 04 '25 08:05 IArnaut2

I meant, this would also do the work

"[html]": {
    "editor.defaultFormatter": "monosans.djlint"
}

almahdi404 avatar May 04 '25 08:05 almahdi404