空白控制
在 Liquid 中,您可以在标签语法中包含连字符 {{-
、-}}
、{%-
和 -%}
,以从渲染标签的左侧或右侧删除空格。
通常,即使不打印文本,模板中的任何 Liquid 行仍会在渲染的 HTML 中打印一个空白行
输入
{% assign my_variable = "tomato" %}
{{ my_variable }}
请注意渲染的模板中“tomato”之前的空白行
输出
tomato
通过在 assign
结束分隔符中包含连字符,您可以从渲染的模板中删除其后的空格
输入
{% assign my_variable = "tomato" -%}
{{ my_variable }}
输出
tomato
如果您不希望任何标签打印空格,作为一般规则,您可以在所有标签的两侧添加连字符({%-
和 -%}
)
输入
{% assign username = "John G. Chalmers-Smith" %}
{% if username and username.size > 10 %}
Wow, {{ username }} , you have a long name!
{% else %}
Hello there!
{% endif %}
没有空格控制的输出
Wow, John G. Chalmers-Smith , you have a long name!
输入
{% assign username = "John G. Chalmers-Smith" -%}
{%- if username and username.size > 10 -%}
Wow, {{ username -}} , you have a long name!
{%- else -%}
Hello there!
{%- endif %}
有空格控制的输出
Wow, John G. Chalmers-Smith, you have a long name!