2

I develop django templates, and I need to indent them with gg=G, but jinja2 tags seem to be immune to it. Here is an example of poorly indented, hard to read if-else tags:

{% extends "base.html" %}
{% block content %}
{% load my_tags %}
<center>
    <form action="#" method="get">{% csrf_token %}
        <input type="text" value="{{ ids }}" name="search_items" size="40"> 
        <input type="submit" value="Search Items">
    </form>
    {% if searching == True %}
    {% if insert_quantity > 0 %}
    <br><i>{{ insert_quantity }} from {{ match_quantity }} matches selected:</i><br>
    {% item_list items %}
    {% else %}
    No such items in database.
    {% endif %}
    {% else %}
    <h2>Item List (10 randomly selected)</h2>
    {% item_list items %}
    {% endif %}
</center>
{% endblock %}

I have installed Glench/Vim-Jinja2-Syntax, but it does nothing. I also use YouCompleteMe, which detects and highlights jinja2 syntax (what makes previous plugin redundant?).

Question: Is it possible to indent those if-else (and for loops) tags with gg=G with some plugin?

  • That plugin just loads the HTML indentation (just like the default htmldjango filetype which comes with Vim, and works quite well for Jinja2 in my experience)... You will probably have to do this yourself, by copying the HTML indent file, and modify it so that it recognized Django-style blocks. The file is over 1,000 lines, so this is probably more involved than one or two extra lines... – Martin Tournoij Aug 25 '15 at 15:51
  • ... Or, easier, just learn to press a <Tab> key after opening a {% ... block, an the <Backspace> when closing one. This works quite well for me... – Martin Tournoij Aug 25 '15 at 15:54
3

The best thing is to indent them yourself with tabs and spaces.

There is one project that is almost what you want, here is a pull request that someone made with auto-indentation for jinja2 templates (I haven't tested, it can be buggy).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.