Liquid Whitespace Control
In Liquid, you can use a hyphen in your tag syntax {{-, -}}, {%-, and -%} to remove whitespace from the beginning or end of a rendered tag. Typically, every line of Liquid in your template results in an empty line in your HTML output, even if it doesn’t display text.
Even if it doesn't output text, any line of Liquid results in a line in your rendered content. By adding hyphens to your Liquid tag, you can remove any whitespace generated by Liquid during rendering.
If you wish to eliminate whitespace from just one side of the Liquid tag, you can include a hyphen on either the opening or closing tag.
{%- if collection.products.size > 0 -%}
The '{{ collection.title }}' collection contains the following types of products:
{%- for type in collection.all_types -%}
{% unless type == blank -%}
- {{ type }}
{%- endunless -%}
{%- endfor -%}
{%- endif -%}
The '{{ collection.title }}' collection contains the following types of products:
{%- for type in collection.all_types -%}
{% unless type == blank -%}
- {{ type }}
{%- endunless -%}
{%- endfor -%}
{%- endif -%}
Whitespace Control
Using hyphens, the assign
statement doesn't produce a blank line.
{%-
assign my_variable = "tomato"
-%}
{{ my_variable }}
{{ my_variable }}