But you can also strip whitespace in templates by hand. If you put an minus sign (-) to the start or end of an block (for example a for tag), a comment or variable expression you can remove the whitespaces after or before that block:
{% for item in seq -%}
{{ item }}
{%- endfor %}
This will yield all elements without whitespace between them. If seq was a list of numbers from 1 to 9 the output would be 123456789.
我找到了python slimmer库,非常适合您需要做的事情。
您还可以研究Jinja's built-in whitespace control,这可能会减少在呈现模板后手动删除空白的需要。
引用the docs:
如果只想去掉多余的空白,可以使用:
或:
如果你想做一些比去掉多余的空白更复杂的事情,你需要使用更强大的工具(或者更复杂的regexp)。
相关问题 更多 >
编程相关推荐