Dexter Adams
Dexter Adams

Reputation: 524

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?

Upvotes: 10

Views: 3612

Answers (2)

Kowsigan Atsayam
Kowsigan Atsayam

Reputation: 453

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

Upvotes: 1

drip
drip

Reputation: 12933

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

Upvotes: 11

Related Questions