Understanding whitespace control in Luquid

Hello! Can somebody clarify why the following code generates only one empty line and not more:

{% assign my_variable = "tomato" %}
{{ my_variable }}

? My understanding is:

  • assign adds \n before itself
  • assign adds \n after itself
  • {{ ... }} adds \n before itself
  • {{ ... }} adds \n after itself
  • jekyll removes two consecutive \n and replaces them with one \n between assign and {{ ... }}

I’ve read this article but it didn’t help.

assign doesn’t add \n before itself or after itself. It simply renders an empty string.
You may test with the following code:

Hello {% assign msg = 'Welcome!' %}  Guest, {{ msg }}

(I’ve intentionally left two spaces between the closing delimiter %} and Guest).

The above template will render into:

Hello   Guest, Welcome!

Note the three spaces between Hello and Guest — one space before assign tag and two after.

Now, the Liquid documentation you’ve linked to in your comment states:

By including a hyphen in your assign closing delimiter, you can strip the whitespace following it…

Let us try it out:

Hello {% assign msg = 'Welcome!' -%}  Guest, {{ msg }}

Now, rendering the above template yields the following result :

Hello Guest, Welcome!
1 Like