Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shopify Liquid Syntax - What is the difference between {%- assign [some_var] = [some_val] -%} and {% assign [some_var] = [some_val] %}

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?

like image 385
Dexter Adams Avatar asked Jan 08 '18 06:01

Dexter Adams


People also ask

What is assign in Liquid?

assign. Creates a new named variable. Input. {% assign my_variable = false %} {% if my_variable != true %} This statement is valid. {% endif %}

What are Liquid tags?

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.

What is a .Liquid file?

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.

What are handles in Shopify?

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.


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 {{- -}}.

like image 82
drip Avatar answered Oct 20 '22 14:10

drip


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 -%})

like image 26
Kowsigan Atsayam Avatar answered Oct 20 '22 12:10

Kowsigan Atsayam