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?
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 -%})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.