8

I work in SublimeText 3. When writing Django templates I have a mixture of html and functions.

I like to indent my code so that block, if and other such statements are indented. For example:

Manual formatting

{% extends "accounts/base.html" %}

{% block content %}
  <h1>Password changed</h1>
  <p>Your password was changed.</p>
{% endblock %}

However, when I run any autoformatter HTML-CSS-JS-Prettify it ignores these brackets and treats them as text:

After formatting

{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
{% endblock %}

Although plugins like Djaneiro give great tag highlighting, I haven't been able to find a way to get SublimeText to treat these as tags.

Has anyone had any luck?

alias51
  • 8,178
  • 22
  • 94
  • 166
  • 1
    There doesn*t *seem* to be any formatter that supports Django at this point. However, you might find this [discussion](https://groups.google.com/forum/#!topic/django-developers/wK2PzdGNOpQ%5B1-25%5D) interesting. – idleberg Aug 31 '19 at 08:58
  • 1
    Actually, [black](https://github.com/psf/black) supposedly supports formatting Django templates. So it sounds like you might be able to use [Sublack](https://packagecontrol.io/packages/sublack), but I haven't been able to get it running myself. Maybe you have more luck! – idleberg Aug 31 '19 at 12:18
  • @idleberg If you are referring to `https://github.com/django/deps/blob/master/accepted/0008-black.rst` unfortunately it only supports python code, not templates. – alias51 Jan 13 '20 at 16:40
  • 2
    Don't know for sublime, but pycharm allows to reformat code by going to Code -> Reformat code and it seems to work reasonably well enough. Maybe you can take a look – ivissani Jan 13 '20 at 21:25
  • 6
    The [**Pycharm**](https://www.jetbrains.com/pycharm/) IDE supports this feature for a long time ago. Fortunately this feature available in both **Professional** and **Community** editions. They already created documentation regarding this feature here, [Reformat and rearrange code--(Pycharm Help)](https://www.jetbrains.com/help/pycharm/reformat-and-rearrange-code.html) – JPG Jan 14 '20 at 10:37
  • Does this thread answer your question? https://stackoverflow.com/questions/22870627/sublime-text-syntax-highlight-jinja2 – nima Jan 17 '20 at 14:08
  • @n1rna Jinja2 is mostly [compatible with Django, but not identical](https://jinja.palletsprojects.com/en/2.10.x/switching/#django) – blkpingu Jan 20 '20 at 11:49

2 Answers2

2

This is a late answer, but I would like to mention a Django template formatter that I've created myself: DjHTML. You can install it using pip install djhtml.

Let's say template.html contains the following:

{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
<script>
$(function() {
console.log("Password changed!");
});
</script>
{% endblock %}

Then running djhtml template.html will give the following output:

{% extends "accounts/base.html" %}
{% block content %}
    <h1>Password changed</h1>
    <p>Your password was changed.</p>
    <script>
        $(function() {
            console.log("Password changed!");
        });
    </script>
{% endblock %}

It's easiest to use DjHTML as a pre-commit hook, so that templates will be automatically indented when you run git commit. Instructions on how to configure pre-commit can be found in the README.

Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42
  • Thanks.:) And it works also with Jinja template in Flask . (It's obvious because Django and Flask uses Jinja template : I write this also for keyword search in S.O.). – phili_b Mar 23 '22 at 20:05
0

There isn't one for sublime text as far as I can tell. I have no source I can quote on this, but I have basically searched nothing came up.

This discussion is by any means old, but active. I found this really old ticket about formatting standards for Django and it has been updated 9 Months ago to basically say they are "in favour of standards" and the proposed formatting for templates would be:

  <ul>
    {% for x in y %}
      <li>{{ x }}</li>
    {% endfor %}
  </ul>

They also made a place happen that holds information about formatting guidelines in Django.

You might find this discussion interesting as well. It's old too, but it highlights the confusion about formatting in Django and the DIY solutions people came up with to cope.

blkpingu
  • 1,556
  • 1
  • 18
  • 41