Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set correct indentation of html jinja blocks in Visual Studio Code?

This is what the code currently looks like:

{% extends "base.html" %}

{% block content %}
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for msg in messages %}
<p>{{ msg }}</p>
{% endfor %}
{% endif %}
{% endwith %}

<h1>{{ title }}</h1>
{% endblock content %}

And this is how I want it to look:

{% extends "base.html" %}

{% block content %}
{% with messages = get_flashed_messages() %}
    {% if messages %}
        {% for msg in messages %}
            <p>{{ msg }}</p>
        {% endfor %}
    {% endif %}
{% endwith %}

<h1>{{ title }}</h1>
{% endblock content %}

I tried changing VSC settings and installing various extensions, but autoformatting code that works perfectly in python code removes indentation in jinja blocks.

like image 454
Krzysztof Avatar asked Nov 03 '25 07:11

Krzysztof


1 Answers

The djlint-vscode extension fixed this for me. You may need to explicitly switch the syntax mode to "Jinja2" instead of "Jinja2 HTML." That should do it. You can manually invoke the indent command with the (default) keyboard shortcut CTRL+SHIFT+I. Also you need to install djlint for whatever Python interpreter you're using.

like image 184
Robbie Capps Avatar answered Nov 04 '25 23:11

Robbie Capps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!