When coding in Shopify’s Liquid language I notice some variables being assigned using the following syntax:
{%- assign variable = value -%}
and other variables being assigned using the following syntax:
{% assign variable = value %}
Could someone explain the difference, if there is any?
2
Answers
When yo use
{% assign variable = value %}
you keep any white space if there is any.But if you use
{%- assign variable = value -%}
the white space is stripped.That’s the main difference.
PS: This is true for any liquid operation
{%- if -%}, {%- capture -%}
etc.. even if you like to output something like so{{- -}}
.In Liquid, you can include a hyphen in your tag syntax {{-, -}}, {%-, and -%} to strip whitespace from the left or right side of a rendered tag.
If you don’t want any of your tags to output whitespace, as a general rule you can add hyphens to both sides of all your tags ({%- and -%})