js-beautify icon indicating copy to clipboard operation
js-beautify copied to clipboard

Add Django template syntax compatibility

Open evenicoulddoit opened this issue 10 years ago • 11 comments

The mustache support is a really nice feature. It would be great if the HTML beautifier also understood (and indented where required) Django template syntax.

Given the following:

{% extends "foo.html" %}{% block bar %}
{% for  in zulu %}<li><a>zulu</a></li>{% endfor %}
{% endblock %}

I would expect the output to be:

{% extends "foo.html" %}
{% block bar %}
    {% for  in zulu %}
        <li>
            <a>zulu</a>
        </li>
    {% endfor %}
{% endblock %}

evenicoulddoit avatar Jun 12 '14 15:06 evenicoulddoit

What you're asking for here is a Django beautifier...

bitwiseman avatar Sep 30 '14 20:09 bitwiseman

There are other templating languages that also uses curly brace markup mixed with html tags, for instance Moustache, Jinja2, and Angular.js

haakenlid avatar Sep 30 '14 23:09 haakenlid

Yes, and those would also be separate beautifiers or at least large feature efforts. :smile: I'm not saying it won't happen, but it is just a significant effort.

bitwiseman avatar Oct 01 '14 04:10 bitwiseman

Sorry, I asked because I thought (can't remember now) that mustache support was included in the HTML beautifier. If that was the case then I assumed that it might make sense for Django template syntax to also be supported (as it's not wildly different). I appreciate the significant effort part, just thought I'd raise as I've found great success in CSSComb, JS-Beautify and I wanted to find something to clean up our template files too :)

evenicoulddoit avatar Oct 02 '14 12:10 evenicoulddoit

We don't "support" any of the template languages fully. But we do make effort towards it over time, and we certainly start with trying not to break the template languages as much as possible.

It looks like someone did add some handlebar support to the html beautifier at https://github.com/beautify-web/js-beautify/commit/499f9b63091a3bcbb5e20d7d8c61a5a36ffddd72 .

If you want to expand on that you are welcome to do so and submit a PR. Thanks!

bitwiseman avatar Oct 02 '14 16:10 bitwiseman

Would love to see this feature too!

jasjuang avatar Sep 27 '16 21:09 jasjuang

Django and related languages do not indent yet, but they are now supported in that they are not killed by beautification.

bitwiseman avatar Jul 18 '19 21:07 bitwiseman

Any method to script this/add manually for project?

antnieszka avatar Jun 05 '20 20:06 antnieszka

I'm also looking for a good solution

AdrienLemaire avatar Jul 09 '20 01:07 AdrienLemaire

@AdrienLemaire @antnieszka @jkevingutierrez PRs welcome. If you look at what is don't for mustache and build around it, it should be possible.

bitwiseman avatar Aug 08 '20 00:08 bitwiseman

It can also be used for liquid templates which use a similar syntax. The only difference is elsif instead of elif.

Full support for these languages isn't necessary, just indenting the parts inside those blocks. It already is indenting the parts within the blocks to the start of the blocks so it's already aware of it. There needs to be one level of indenting for every block content.

This is the default formatter in vscode so it would be good to use it because people using these templating languages have to disable it.

Or can we get an option to preserve everything in between certain tags? Then if we specify {% and %} it won't ruin the indenting inside it. There's already an option to preserve the wrapping of html attributes. It wouldn't be ideal but would at least let us keep the formatter on.

rightaway avatar May 10 '23 17:05 rightaway