模板块以“{%-”或“-%}”符号开始或结束
template blocks start or end with "{%-" or "-%}" symbols
我在 GitHub 上阅读 "flask_bootstrap/templates/bootstrap/base.html",发现一些奇怪的块符号,例如 {%- block ... %}
或 {% endblock ... -%}
。为什么{%
或%}
前后有“-”?
它会使块声明与正常的不同吗?我要扩展这个文件,所以我想详细了解一下。
该特定功能(空白控制)不是来自 Flask 服务器,而是来自 Jinja2 template engine。
来自链接文档:
You can also strip whitespace in templates by hand. If you add a minus
sign (-
) to the start or end of a block (e.g. a For tag), a
comment, or a variable expression, the whitespaces before or after
that block will be removed:
{% for item in seq -%}
{{ item }}
{%- endfor %}
我在 GitHub 上阅读 "flask_bootstrap/templates/bootstrap/base.html",发现一些奇怪的块符号,例如 {%- block ... %}
或 {% endblock ... -%}
。为什么{%
或%}
前后有“-”?
它会使块声明与正常的不同吗?我要扩展这个文件,所以我想详细了解一下。
该特定功能(空白控制)不是来自 Flask 服务器,而是来自 Jinja2 template engine。
来自链接文档:
You can also strip whitespace in templates by hand. If you add a minus sign (
-
) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed:{% for item in seq -%} {{ item }} {%- endfor %}