10

I have a Jinja2 template that I'd like to use, but can't in conjunction with prettifying the code. This question has been asked before, but that hasn't had an answer in almost 2 years, so perhaps there's an answer out there now.

<select id="example-getting-started" name="test" multiple="multiple">
   {% for k in pizza_dict %}
      <option value="{{ k }}" {% if pizza_dict[k] %}selected{% endif %}>{{ k }}</option>
   {% endfor %}
</select>

BeautifulSoup and lxml will "prettify" this code to:

<select id="example-getting-started" multiple="multiple" name="test"> 
{% for k in pizza_dict %} 
    <option endif="" if="" pizza_dict="" value="{{ k }}"> {{ k }} </option> 
{% endfor %} </select>

which will destroy the code's function.

Community
  • 1
  • 1
Michael K
  • 2,196
  • 6
  • 34
  • 52
  • 1
    Jinja templates aren't xml so I wouldn't expect an xml prettifier to work. – tdelaney Apr 17 '15 at 17:37
  • You could try to use BeautifulSoup. See [this](http://stackoverflow.com/questions/6150108/python-how-to-pretty-print-html-into-a-file) answer. Also read [this](https://groups.google.com/forum/#!topic/pocoo-libs/s8CKOjz8J_0) topic. – doru Apr 19 '15 at 07:46
  • @doru I demonstrated in my question that beautifulsoup and lxml both don't work. – Michael K Apr 19 '15 at 14:06
  • 2
    As mentioned [here](https://stackoverflow.com/a/50099477/9704865), I use the [atom-beautify](https://atom.io/packages/atom-beautify "atom-beautify") package for the [Atom](https://atom.io/ "Atom") text editor when developing Flask/Jinja2 applications. The formatted templates look really neat. – Nimrod Apr 30 '18 at 11:28
  • @Nimrod thanks, that’s a great solution. – Michael K Apr 30 '18 at 16:03

2 Answers2

3

VSOT (see Rodrigo's answer) seems to be unmaintained, but they recommend two alternatives:

vauhochzett
  • 2,732
  • 2
  • 17
  • 40
2

Recently I was seeking for some tool for autoformatting and found the VSOT: https://pypi.org/project/vsot/

Rodrigo
  • 135
  • 4
  • 45
  • 107