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