在 Liquid 模版中,你可以將連字符放在標記(tag)中,例如?{{-
、-}}
、{%-
?和?-%}
,用于將標記(tag)渲染之后的輸出內容的左側或右側的空拍符剔除。
通常,即使不輸出文本,模版中的任何 Liquid 表達式仍然會在渲染之后輸出的 HTML 中包含一個空行:
輸入
{% assign my_variable = "tomato" %}
{{ my_variable }}
請注意渲染之后輸出的 “tomato” 字符前面包含了一個空行:
輸出
tomato
通過為?assign
?標記(tag)添加連字符,可以將渲染之后所輸出的空拍符刪除:
輸入
{%- assign my_variable = "tomato" -%}
{{ my_variable }}
輸出
tomato
如果你不希望任何標記(tag)被渲染之后所輸出的內容有任何空白符,只需在所有標記(tag)兩側全部添加連字符即可,例如 ({%-
?和?-%}
):
輸入
{% 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!
? Copyright 2023 深圳藍曬科技有限公司. 粵ICP備2023054553號-1