最佳做法

條件式屬性周圍的空格

有時會用條件判斷為標籤加上 class。djLint 會移除條件陳述式內的空白。

建議使用這種寫法:

<div class="class1 {% if condition -%}class2{%- endif %}">content</div>
                  ^ space here

不建議使用這種寫法:

<div class="class1{% if condition -%} class2{%- endif %}">content</div>
                                     ^ space here

format_attribute_template_tags 與去除空白的條件式屬性

如果啟用了 format_attribute_template_tags 選項,條件式屬性應使用去除空白的標籤,例如 nunjucks 和 jinja 中的 {% if a -%},以移除標籤內的空格。

djLint 會將過長的屬性格式化成多行,而保留在屬性內的空白可能會破壞你的程式碼。

建議使用這種寫法:

<input value="{% if database -%}{{ database.name }}{%- else -%}blah{%- endif %}" />
                        ^                       ^      ^        ^

每個標記的位置都是去除空白的標籤。

不建議使用這種寫法:

<input value="{% if database %}{{ database.name }}{% else %}blah{% endif %}" />

格式化之後可能會變成這樣:

<input
  value="{% if database %}
                  {{ database.name }}
              {% else %}
                  blah
              {% endif %}"
/>