美化 Jinja2 模板

Mic*_*l K 8 html python jinja2

我有一个想要使用的 Jinja2模板,但不能与美化代码结合使用。之前有人问过这个问题,但在将近 2 年的时间里没有答案,所以现在也许有答案了。

<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>
Run Code Online (Sandbox Code Playgroud)

BeautifulSoup 和 lxml 会将这段代码“美化”为:

<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>
Run Code Online (Sandbox Code Playgroud)

这将破坏代码的功能。

vau*_*ett 3

VSOT(参见Rodrigo 的回答)似乎没有维护,但他们推荐了两种替代方案: