js-beautify
js-beautify copied to clipboard
Add Django template syntax compatibility
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 %}
What you're asking for here is a Django beautifier...
There are other templating languages that also uses curly brace markup mixed with html tags, for instance Moustache, Jinja2, and Angular.js
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.
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 :)
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!
Would love to see this feature too!
Django and related languages do not indent yet, but they are now supported in that they are not killed by beautification.
Any method to script this/add manually for project?
I'm also looking for a good solution
@AdrienLemaire @antnieszka @jkevingutierrez PRs welcome. If you look at what is don't for mustache and build around it, it should be possible.
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.