Search code examples
pythonhtmljinja2

Prettify Jinja2 Template


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.


Solution

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