【问题标题】:Prettify Jinja2 Template美化 Jinja2 模板
【发布时间】:2015-06-24 16:39:02
【问题描述】:

我有一个我想使用的 Jinja2 template,但不能与美化代码结合使用。 This question 之前有人问过,但已经有将近 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>

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>

这会破坏代码的功能。

【问题讨论】:

  • Jinja 模板不是 xml,所以我不希望 xml 美化器起作用。
  • 你可以尝试使用 BeautifulSoup。请参阅this 答案。另请阅读this 主题。
  • @doru 我在我的问题中证明了 beautifulsoup 和 lxml 都不起作用。
  • 正如here 提到的,我在开发Flask/Jinja2 应用程序时使用atom-beautify 包作为Atom 文本编辑器。格式化的模板看起来非常整洁。
  • @Nimrod 谢谢,这是一个很好的解决方案。

标签: python html jinja2


【解决方案1】:

最近我正在寻找一些用于自动格式化的工具并找到了 VSOT:https://pypi.org/project/vsot/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多