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?
assign. Creates a new named variable. Input. {% assign my_variable = false %} {% if my_variable != true %} This statement is valid. {% endif %}
Liquid tags are special elements of the Forem Markdown editor. They are custom embeds that are added via a {% %} syntax. Liquid is a templating language developed by Shopify. Liquid embeds are for tweets, like {% tweet 765282762081329153 %} or a Forem user profile preview, like {% user jess %} etc.
A liquid file is a mix of standard HTML code and Liquid constructs. It's an easy to read syntax, and is easy to distinguish from HTML when working with a Liquid file. This is made even easier thanks to the use of two sets of delimiters.
The handle is used to build the URL for the resource, or to return properties for the resource. Other objects like linklists , links , and settings also have handles.
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 -%})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With