liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Request for suggestions: Liquid comments

Open tobi opened this issue 5 years ago • 36 comments

Right now comments in liquid are implemented through the {% comment %} tag. Basically a block that doesn't do anything.

This has always been clunky and I think we should introduce syntax for this.

This is especially true because of the new liquid block tag. Consider that this is the only way to do inline comments right now:

{% liquid
comment Is the feature_image set? 
endcomment

if product.featured_image
  echo product.featured_image | img_tag
else
  echo 'product-1' 
endif 
%}

Which... is bad.

My current thinking is that we should double down on our ruby heritage and designate # as the comment character. Alternatives here would be c style // or lua style --

This would transform the above to

{% liquid
# Is the feature_image set? 
if product.featured_image
  echo product.featured_image | img_tag
else
  echo 'product-1' 
endif 
%}

Additionally we could introduce a new inline token for comments: Single line comment {# comment #} and multiline comments would be both supported

{# 
  First line 
  Second line
#}

The downside of this syntax is that it may cause collisions in existing liquid code. We can (but haven't) check the Shopify databases for how common this might be.

tobi avatar Jan 29 '21 21:01 tobi

There are some advantages in consistency if we define the # character (or //, -- etc) as a standard Liquid tag. This would require only a small change to the core grammar, and would give us most of the other things we would want for free.

Examples:

{% # using a wrapper element for IE compatibility %}
<div class="foo">
  ...
{% liquid
# Support for the Liquid tag comes for free, since comments are just
# standard tag calls. 
if product.featured_image
  echo product.featured_image | img_tag
else
  echo 'product-1' 
endif 
%}
{% # this sort of comment also
   # will just work, because it parses
   # as a single call to the "#" tag %}

samdoiron avatar Jan 29 '21 21:01 samdoiron

OK that makes perfect sense and means that we don't need a new tag at all.

The one question is whitespace management. I'd like for comments to imply {%- -%} whitespace eating. Perhaps we can just introduce (or do we already do that?) a rule that a tag that doesn't output anything itself always eats surrounding whitespace. I imagine this would change resulting HTML, especially with common if and for tags. But... that's probably good?

HTML doesn't care much about whitespace but plain text does. Many use cases of liquid exist around plain text, so if we change the default we definitely need a fallback.

tobi avatar Jan 30 '21 17:01 tobi

In my long life of writing code, I have almost never run into trouble using the hash # symbol to introduce a comment, but I have had a lot of trouble with the double-slash // and its block companion /* */ so I am all over this introduction of a single tag symbol to open a comment. Also, the new liquid block is sexy. Super nice and also somewhat amazing that it took so long to evolve! You know it is right when it just feels right and not forced.

resistorsoftware avatar Jan 30 '21 18:01 resistorsoftware

I would prefer to use # because the same work with shopify scripts, more towards ruby comments.

single line comment should work with # multiple line comment can still with {%comment%} {% endcomment%}

ashukasma avatar Jan 30 '21 18:01 ashukasma

Since templating engines "inject code," it makes sense to use # like we do in Ruby, for consistency reasons. I also like @samdoiron 's suggestion as in the following,

{# 
  First line 
  Second line
#}

First line and Second line might be wrongly interpreted as expressions since # conventionally means "ignore everything after this line"

0kzh avatar Jan 30 '21 18:01 0kzh

Definitely prefer to use #. I agree with @ashukasma that it keeps some consistency with Shopify scripts.

I think it make sense to use {# comment #} instead of doing {% # comment %} as you now have to deal with white space control which will cause you to have to do {%- # comment -%}. Instead you could just have {# comment #} remove whitespace by default.

Using {# comment #} also allows syntax highlighting to easily pickup on a whole block of comments.

CalinR avatar Jan 30 '21 19:01 CalinR

OK that makes perfect sense and means that we don't need a new tag at all.

The one question is whitespace management. I'd like for comments to imply {%- -%} whitespace eating. Perhaps we can just introduce (or do we already do that?) a rule that a tag that doesn't output anything itself always eats surrounding whitespace. I imagine this would change resulting HTML, especially with common if and for tags. But... that's probably good?

HTML doesn't care much about whitespace but plain text does. Many use cases of liquid exist around plain text, so if we change the default we definitely need a fallback.

I never understood why whitespaces in control tags were a thing. Having 80+ white lines where a for used to be adds nothing but extra payload to the resulting document. They should all eat whitespaces by default.

I agree on the {% # , the reason why {# is not viable is because it will break inline CSS. E.g: @media(...){#mydiv will break the parser.

bworwa avatar Jan 30 '21 19:01 bworwa

Yea, you are right that {# can occur naturally pretty easily. So introducing whitespace clobbering on {% # comment only tags %} feels safe enough.

tobi avatar Jan 30 '21 19:01 tobi

Using {# comment #} also allows syntax highlighting to easily pickup on a whole block of comments.

This is just as easy with {% # this syntax %} since tags can span multiple lines. For example:

{% # 
  Multi-line comments could look like this,
  because tags can span multiple lines.
%}

I never understood why whitespaces in control tags were a thing. Having 80+ white lines where a for used to be adds nothing but extra payload to the resulting document. They should all eat whitespaces by default.

Totally agree, especially in this case. From memory this might be as simple as defining blank? to return true on the node.

samdoiron avatar Jan 30 '21 19:01 samdoiron

I agree on the {% # , the reason why {# is not viable is because it will break inline CSS. E.g: @media(...){#mydiv will break the parser.

Excellent point. This point alone is good enough to rule out the {# syntax and instead go with {% # to avoid conflicts like this.

#This is just as easy with {% # this syntax %} since tags can span multiple lines. For example:

Good point, this seems like a non issue then.

CalinR avatar Jan 30 '21 20:01 CalinR

Definitely agree with @samdoiron on {% # this syntax %}.

Less certain that eating whitespace should be the default behaviour. Two main concerns:

  • It's not the standard behaviour of other Liquid tags.
  • It can cause unexpected HTML-breaking issues in certain situations. One example:

Liquid:

<video class="foo"
  autoplay
  {% # a comment explaining next line %}
  muted
  src="bar.mp4"
></video>

Invalid output (with auto whitespace removal):

<video class="foo"
  autoplaymuted
  src="bar.mp4"
></video>

Desired output:

<video class="foo"
  autoplay
  muted
  src="bar.mp4"
></video>

Changing {% # to {%- # -%} when typed should be left to IDE shortcuts and user preferences imo.

isAdrisal avatar Jan 31 '21 01:01 isAdrisal

I could be wrong, but my understanding is that the way whitespace control currently works is that the output would actually be what you showed in the desired output. It should act the same as a {%- where it would just remove the line that the code block was on and not remove the line break before it.

CalinR avatar Jan 31 '21 01:01 CalinR

Perhaps I've misunderstood how this will behave differently to {%- comment -%}{%- endcomment -%}.

Currently, this input:

<video class="foo"
  autoplay
  {%- comment -%}A comment{%- endcomment -%}
  muted
  src="bar.mp4"
></video>

gives this output:

<video class="foo"
  autoplaymuted
  src="bar.mp4"
></video>

isAdrisal avatar Jan 31 '21 01:01 isAdrisal

You are 100% correct. Sorry I don't know why I thought it behaved that way. I just tested it out myself too. I agree, that is a bit weird and I guess my misunderstanding shows that people would probably run into that problem often if that was auto assumed.

You're probably right with having it be controlled by the user instead. Other way would be to have the behaviour changed specifically for this instance, but that could possibly produce unexpected results if it was on the same line as other text.

CalinR avatar Jan 31 '21 01:01 CalinR

are 100% correct. Sorry I don't know why I thought it behaved that way.

I think the assumption that tags don't change data outside of their bounds is the sane one. I can imagine the reasoning for it being to remove extra lines that exist only for comments. But it's definitely weird.

I don't think anyone would want that behaviour of {%- to be the default for comments.

The behaviour I would expect for a comment is that everything from the opening { to the closing } is striped out entirely. The {%- syntax seems to This may be possible without differing too much from existing behaviour.

samdoiron avatar Jan 31 '21 04:01 samdoiron

The behaviour I would expect for a comment is that everything from the opening { to the closing } is striped out entirely. The {%- syntax seems to

Would that be similar to the current {% comment -%}{%- endcomment %} behaviour, with whitespace only removed for the inside edges of the tags?

This may be possible without differing too much from existing behaviour.

I don't feel especially strongly about this, but I am generally not in favour of creating new edge cases where certain language rules/behaviours don't apply. Given not stripping whitespace is the default for all other Liquid tags, I'd prefer it be the default here also and {%- syntax is always available to change that based on preference.

Edit to add: I should clarify, I'm totally fine with {% # this syntax %} matching the whitespace behaviour of {% comment -%}this syntax{%- endcomment %}.

Liquid:

<video class="foo"
  autoplay
  {% comment -%}
    A comment that spans
    across
    multiples lines.
  {%- endcomment %}
  muted
  src="bar.mp4"
></video>

Output:

<video class="foo"
  autoplay
  
  muted
  src="bar.mp4"
></video>

isAdrisal avatar Jan 31 '21 04:01 isAdrisal

I don't have a strong opinion on this, but I'll add some notes about my decision process when adding support for Liquid comments in my own app (which has a preprocessing step to remove such comments before strings are parsed with Liquid).

I use {# ... #}, which I chose so that comments have an ending ( #} ) that is distinct from Liquid's tag ending ( %} ).

This means comments can contain any sequence of characters (other than #} of course), including Liquid tags, allowing them to be used for commenting out sections of Liquid code (including incomplete code that would break the parser if {% comment %}...{% endcomment %} was used).

As far as {# occurring naturally in code (as noted above), it's less likely the code will also contain #} to end the comment. Without a matching ending, the {# sequence can just be left alone (which is how my app handles it, for this very reason).

nickpearson avatar Jan 31 '21 05:01 nickpearson

I'll throw my support in for the {% # Comments! %} (and {%- # Stripping comments! -%}) syntax as well. The basic tag should do the same non-stripping of whitespace that existing tags do. Whitespace stripping is great for blocks but can do weird stuff when used in the middle of a line or HTML tag, and as an inline comment is well-suited to being in either of those places I vote that the whitespace control should be left for the author to decide.

DavidBilsky avatar Feb 01 '21 14:02 DavidBilsky

I think the assumption that tags don't change data outside of their bounds is the sane one. I can imagine the reasoning for it being to remove extra lines that exist only for comments. But it's definitely weird.

While the big swathes of empty space from the default whitespace behavior (as with ERB) always makes my eyes jump, I agree with @samdoiron. I’d keep this change as narrow as possible and just introduce # as a comment char to EOL, leaving the behaviors of the {%...%} and {%-...-%}tokens as is. At least until you’re ready to bundle it in with other breaking changes that would be net usability improvements.

duncan avatar Feb 01 '21 16:02 duncan

Update: My original post proposed using # but I've since updated to using ~ instead. Note that the ~ is only one possible character to demonstrate the concept. If it conflicts with other languages, a different character or phrase of characters can be used.

I fully support this! Please implement. Writing comments in Liquid is the most cumbersome way of comment-writing that I ever encountered. (And once I thought <!-- HTML comments --> are weird!) It's so natural to use // and /* */ but they scream JavaScript/CSS to me (plus writing them mixed with Liquid would be a readability nightmare), so ~ is the perfect comment character in Liquid.

Concept 1

My idea would be to use the {} with the ~ because the curly brackets already have a meaning in Liquid. It agrees with @nickpearson as well as @samdoiron and it expands further:

{% ..... %} - processing tag opener and closer {{ ..... }} - output tag opener and closer {~ ..... ~} - multi-line comment opener and closer {~} ... EOL - single line comment indicator (see note below) ~ ..... EOL - single line comment tag (for use in liquid tag only as a shorthand for {~})

Example: Shopify liquid new comment proposal tilde

Pseudo code logic is simple!

  • Where Liquid already looks for {% and {{, add one more case to look for {~.
  • If found, then check the next character right after that.
  • If that's a } ignore everything until the end of line.
  • If not, then look for the closing ~} and ignore everything in between.
    • Also look for {~ that comes first (but skip {~}), then recursively look for both {~ and ~} until all nested comments are closed. This allows commenting out huge blocks of Liquid code for testing.
  • Inside liquid tag, have a logic looking for ~ or {~} as a tag anywhere within the Liquid code and if found, ignore everything until EOL
    • Also look for nested multi-line comments inside the liquid tag using the exact same logic as those outside.

This to me would be the perfect way to introduce real comments. It gels very well with standard programming practice, and it fully agrees with how everything else is currently implemented in Liquid. It also won't break any existing Liquid code (to the best of my knowledge). It will also work great with syntax highlighters.

I don't know any other language that has a processing keyword (tag) like comment other than the infamous REM simply to mark comments. They just use special characters like //, #, /* */, etc. so {~}, ~ and {~ ... ~} would perfectly align with this standard.

Whitespace Control Just like other openers and closers, the multi-line comment opener and closer would support white space control using {~- and -~}. The single line comment indicator {~} should make whitespace stripping mandatory (towards the left only, not after EOL), and won't have any hyphenated form.

Why is the single line comment indicator {~} and not ~? I chose to use {~} to indicate single line because using ~ as an indicator would be a breaking change and it departs from the established norm that all Liquid code is enclosed in {}. I also won't use {~ because then the parser has to look for the multi-line closer ~} then go back and look for EOL when it doesn't find one. This is extra unnecessary work and makes the logic more complex. (Same reason why // ... // doesn't exist.) Also, syntax highlighters would be confused where the closing } is.

PS: The link on "the infamous REM" is a great read about the history of programming comments. Be sure to read it! (Should I add a mega-comment?)

@dylanahsmith I took like an hour or two writing this, and then I saw your PRs being referenced here. I hope you'll take a look and see if this is a good idea and it can be implemented.

Edit: Tagging Shopify/liquid-c#144 and #1401

ADTC avatar Feb 19 '21 16:02 ADTC