是否可以在WebStorm中为Django / html模板设置自动缩进?

时间:2016-04-18 16:20:42

标签: python html django webstorm code-formatting

问题:当我自动意图时,它会删除模板中任何标记/ python逻辑的所有缩进。

是否有一个插件可以为Django模板提供自动缩进功能?如果没有,我如何编辑Auto-indent Lines命令⌃⌥I以支持下述格式:

的index.html:

{% block title %}
  {# note the indent here with django logic #}
  {% render title_tag with title="Sign up Now" global=global %}
{% endblock %}

{% block description %}
  {# note the indent here with html markup #}
  <meta name="description" content="Sign up today with our product.">
{% endblock %}

{% block body %}
{# note the indent here If statement #}
{% if true %}
  <section class="section">
    <p> here too</p> 
  </section>
  {% if false %}
    <div>
      <p>wowo</p>
    </div>
   {% endif %}
{% endif %}

1 个答案:

答案 0 :(得分:1)

<强>答案:

为了正确地格式化这些东西(例如在块内保留缩进),IDE必须理解Django标记。 WEB-7814 ticket正在询问(明星/投票/评论以获得任何进展通知)。

现在我可能只建议尝试安装“Twig Support”插件(应该与WebStorm一起使用)。 Twig使用非常相似的语法(灵感来自Django),因此应该在这里提供帮助。否则 - 使用Python插件的PyCharm或完整的IntelliJ。

Twig插件:插件安装指南+格式化+用法

  • 打开:主菜单&gt; Webstorm&gt;首
  • 搜索“插件”

Step 1 Step 2 Step 3

  • 安装Twig支持。
  • 重新启动Webstorm。
  • 打开:主菜单&gt; Webstorm&gt;偏好。
  • 搜索“Twig”
  • 转到编辑器&gt;代码风格&gt;枝条 Step 4
  • 将标签大小和缩进设置为2。
  • 按申请。
  • 搜索“文件类型”
  • 点击“编辑器&gt;文件类型” Step 5
  • 转到“已识别文件类型”窗格内的“Twig”。 Step 6
  • 点击“+”并添加“ .html”“ .tpl”注意:您可能会收到错误消息,提示此文件扩展名已经存在 用在哪里。覆盖以前的用法。

Step 7

  • 点击“应用”。
  • 重新启动Webstorm。

Auto indent