83

What is the correct way to comment out in the Liquid templating language?

Martin Buberl
  • 45,844
  • 25
  • 100
  • 144

6 Answers6

132

In Liquid you comment out using the {% comment %} and {% endcomment %} tags:

{% comment %} This is a comment in Liquid {% endcomment %}

It doesn't matter if the comment is inline or a block comment.

{% comment %}
    This is a block comment in Liquid
{% endcomment %}
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
  • 21
    This is a surprisingly verbose and non-ergonomic syntax. I was expecting to be able to do something like line `/* Fnord */` inside a `{% %}` run, e.g. `{% elseif /* do the other thing:*/ %}`. That's a shame. – Dai Feb 22 '19 at 12:50
  • 1
    this does not work properly for commenting out say an `{% if .. %} ` so its not only verbose, its a bit trashy too – Toskan Aug 13 '21 at 21:14
  • 1
    correct answer is the one with only a few upvotes below: https://stackoverflow.com/a/64867876/533426 – Toskan Jul 25 '22 at 09:56
20

If, like me, you are looking for a solution that actually comments out "anything"/everything between the comment tags (as described in the documentation), you can use the {% raw %} tag (in conjuction with the {% comment %} tag if you don't want anything rendered in the browser).

Example:

{% comment %}
    {% raw %}
        Here is some text that I don't want displayed and
        {% some_liquid_stuff_that_I_don't_want_parsed %}
    {% endraw %}
{% endcomment %}

will render nothing at all.

In contrast,

{% raw %}
    Here is some text that I want displayed but
    {% some_liquid_stuff_that_I_don't_want_parsed %}
{% endraw %}

will render

Here is some text that I want displayed but

{% some_liquid_stuff_that_I_don't_want_parsed %}

while

{% comment %}
    Here is some text that I don't want displayed but
    {% some_liquid_stuff_that_will_be_parsed %}
{% endcomment %}

may result in a syntax error or Liquid exception, depending on the validity of the Liquid inside the comment tags.

An example of where this becomes an issue is where some work-in-progress code has been commented out:

{% comment %}
    {% if some test %}
         some stuff to render
    {% elsif... %}
         unfinished code...
{% endcomment %}

(In this case you may end up with an unfinished if statement error.)

Additional information on this GitHub thread.

Luke
  • 4,825
  • 2
  • 30
  • 37
7

Liquid allows you to leave un-rendered code inside a Liquid template by using the {% comment %} and {% endcomment %} tags.

Input:

Anything you put between {% comment %} and {% endcomment %} tags
is turned into a comment.

Output:

Anything you put between  tags
is turned into a comment.

Reference documentation: Comment tag in Liquid

okconfused
  • 3,567
  • 4
  • 25
  • 38
  • 2
    I really enjoyed the way you did this example so it made readable sense in the input and the output. – Luke May 16 '19 at 10:36
7

Starting with Liquid 5.4.0 you will be able to use a short inline comment that does not require a closing tag! The syntax is:

{% # This is a new inline comment! %}

As with other tags you can add hyphens to remove whitespace around it:

{%- # This is a new inline comment without whitespace! -%}

And even use multiple lines:

{%- 
################################
#  This is a really big block  #
################################ 
-%}

More info is available in the merged PR.

Boltgolt
  • 232
  • 2
  • 10
2

In liquid, you use {% comment %} and {% endcomment %} tags:

{% comment %} This would be commented out {% endcomment %}

You can also use it in block:

{% comment %}
    This would also be commented out
{% endcomment %}

If the {% comment %} and {% endcomment %} tags would comment anything, including HTML elements and such:

 {% comment %}
    <div class="commented_out">
    <p>This whole div would be commented out</p>
    </div>
{% endcomment %}
Zeusox
  • 7,708
  • 10
  • 31
  • 60
1

In the liquid, using comment tag enclose the text to be commented inside the comment tag

{%comment%}
Text to be commented
{%endcomment%}