Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible Improvement to Comments #8

Closed
thegrandpoobah opened this issue Apr 26, 2011 · 26 comments
Closed

Possible Improvement to Comments #8

thegrandpoobah opened this issue Apr 26, 2011 · 26 comments
Labels

Comments

@thegrandpoobah
Copy link

In the spec, the syntax for Comments states that comments are opened with '{{!' and closed with '}}', the consequence being that '}}' cannot appear inside comments. That's fine and works fairly well. But one thing I've found useful during everyday coding is to comment out Mustache code fragments using Mustache comments. Our "Mustache-like" parser achieves this by using '{{!' as the open token and '!}}' as the close token. So in our parser, when you have a code fragment like:

{{#foo}}{{bar}}{/foo}}

you can easily comment it out via:

{{! TODO: Determine why this code fragment does not work {{#foo}}{{bar}}{{/foo}} !}}

A similar thing cannot be achieved with a spec-compliant parser which I think is a shame.

As far as I understand it, this request would be backwards compatible too since existing code must not nest '}}' in comments.

What do you guys think?

@pvande
Copy link
Contributor

pvande commented Apr 26, 2011

It seems like a change like this would make Comment tags mirror Set Delimiter tags in implementation.

I presently dislike that we have to deal with special cases like this, though I can see the value in being able to comment out tags, and I'd rather not try to solve the delimiter counting problem.

I've thrown the question out to a wider pool, but I think I'm +1 to including this in v2.0.0 of the spec.

@agibralter
Copy link

Hmm... I'm down for a +1 here. Makes sense.

@groue
Copy link

groue commented Apr 27, 2011

I'm -1, for the following reasons:

  • this change would have a negative impact on parsing performance.
  • humbleness: carefully-designed languages like C (and its siblings, Java, etc), or XML do not allow nested /* */ comments.

Yet, the problem is real. It's difficult, in today's mustache syntax, to quickly comment out a whole bunch of template lines.

Since the problem is real, it's not new. And solution has already been found by other languages. Namely : the single-line comment, introduced by // in C++ and javascript, # in shell and ruby, etc.

This is the direction we should aim at.

@thegrandpoobah
Copy link
Author

Groue, can you elaborate on why this would have a negative performance impact? The change can still be matched with a regular expression:

/{{![\s\S]*?!}}/ in JavaScript for multiline matches.

And just to clarify this would not allow nesting of comments. The following would still be bad:

{{! outside comment {{! inside comment !}} <-- outside comment gets terminated there, not here --> !}}

Which actually resembles the parsing behaviour of the following C#, C++, and C code perfectly:

/* outside comment /* inside comment */ <-- outside comment gets terminated there, not here --> */

I hope that this clarifies the original suggestion a bit.

@groue
Copy link

groue commented Apr 27, 2011

I hope that this clarifies the original suggestion a bit.

I admit I missed the exclamation mark in your proposal for a new comment closing !}} tag, and that there is no request for nested comments in your initial writing. My apologies.

But now, I don't get how you're planning to keep compatibility, if you change the comment closing tag : {{! old-style comment }} Am I still in the comment now ?

@thegrandpoobah
Copy link
Author

I think what I really meant to say with that line about backwards compatibility is that the existing Mustache code is forwards compatible with this change. new-style comments (assuming they are not commenting out Mustache code) would be acceptable old-style comments since older parsers would treat the last ! as just another character. But you are right, it seems this suggestion loses backwards compatibility in its present form. I can think of ways of fudging a parser to work correctly with both syntaxes, but they all have a hack-ish taste (balanced parenthesis matching, longest match, etc).

Another token for this construct perhaps?

@groue
Copy link

groue commented Apr 27, 2011

Keeping my -1 until a solid proposal has been written, then. Also, let's keep in mind that :

  • this issue has been opened because of an inconvenience, not because the mustache syntax has any bug. This should help relativize the urgency of the issue.
  • mustache is a cross-language template syntax, and we should strive for compatibility among the many implementations out there: https://github.com/defunkt/mustache/wiki/Other-Mustache-implementations. It wouldn't be politically wise to rush extending the mustache spec before gathering implementors' opinions.

@agibralter
Copy link

Also, agree that it's not that big of an issue. If the main use case is just to be able to quickly comment out sections of mustache templates... well... maybe a vim plugin is the way to go?

@pvande
Copy link
Contributor

pvande commented Apr 27, 2011

Since there seems to be some confusion, let's refocus a bit:

  • This ticket was opened to offer a backwards-incompatible language improvement, not a bug.
  • The Mustache specification is semantically versioned specifically to permit backwards-incompatible language changes without alienating spec-compliant implementations.
  • When v2.0.0 of the spec is released, there will still continue to be improvements to the v1.x versions, and parsers for both versions will still have their places.

There has been a concrete proposal made: the {{! comment }} syntax be replaced by {{! comment !}}.

This change offers a tangible benefit: comment tags cannot enclose other mustache tags, which is an inconvenience.

@defunkt
Copy link
Member

defunkt commented Apr 27, 2011

👍

Many people expect this to work, but it does not.

Comments are already a special case so I think the closing bang is a-ok.

@groue
Copy link

groue commented Apr 27, 2011

Should we keep on discussing the alteration of the comment syntax, I would keep investigating the one-line comment, since it would solve both the "comment tags cannot enclose other mustache tags" inconvenience, and the nested comments that some other users will eventually ask for, too.

This is not a comment {{!! the rest of the line, regardless of }}, {{, !, etc. is a comment
This is not a comment

Now in order to comment a bunch of lines, with tags, with comments, anything, we just have to prepend each line with {{!! (just like we do in our regular programming languages).

(if {{!! does not look good to you, we can change, I have no opinion on the single-line comment token.)

@defunkt
Copy link
Member

defunkt commented Apr 27, 2011

@groue I'm curious about how you're using Mustache. I see you've written an Obj-C implementation — what sort of templates are you Mustaching?

I ask because to me the single line comment feels very un-HTMLy, where all carets are balanced. But maybe that's too narrow a perspective.

@groue
Copy link

groue commented Apr 27, 2011

@defunkt: I'm using it, as many, to render HTML. iOS ships with a remarkable UIWebView, but no templating tool for rendering the HTML. I developed GRMustache for this only purpose, and I'm quite glad you did whole the hard work of setting up the {{mustache}} core. I was quite happy to use its advanced features, namely partials and lambda sections. Some users of GRMustache came to it because they were already there, ready to be unleashed.

Mustache, with its default HTML escaping, looks like it is a HTML-oriented templating language. I think this is only a convenience, a safe default, but certainly not something which should taint mustache forever. Where, in its core concepts of tags and their relationship with the rendered data, would lie any HTML-focused property? I may well one day generate some Python with it, or some PDF, and maybe not with my Obj-C implementation. I mean, who knows?

Now, on the comments syntax discussion. @thegrandpoobah asked for comments that would enclose other mustache tags. This is feature request [1]. In other languages, it's quite common that people ask for nestable comments. This is feature request [2].

In the language history I'm aware of, [1] is not common, since the comment syntax is usually very distinct. It's obvious that /* and */ easily encloses other C statements, because those delimiters can not be seen in C statements.

But in the same language history, [2] is very common. And the rest-of-the-line comment has been the cleverest solution to it. There is no ambiguity, it comments anything, it's easy to nest comments, and as @agibralter mentioned, text editors can trivially provide with quick commenting of whole file sections.

The reason why I focus on this [2] stuff, the nested comments, and the rest-of-the-line comment solution, whereas nobody here seems to care, is that I know for sure that some people will care one day, and that it solves [1] altogether.

I hope I've been clearer. I'd with much joy throw away HTML-like balanced syntax, when I consider the benefits of rest-of-the-line comments.

@groue
Copy link

groue commented Apr 27, 2011

Also, I think everybody here has been mad, at least once, at the balanced HTML/XML comments :-)

@groue
Copy link

groue commented Apr 27, 2011

Last point: I strongly support that Mustache keeps its {{! local comment (!)}}. Rest-of-the-line comments would be an extension to Mustache syntax, certainly not a replacement for the balanced comment tags we're already used to.

@thegrandpoobah
Copy link
Author

Certainly if you use the evolution of C as a case study, both /* */ and // have their utility and place in the language. Since both ideas have merit IMO, why not split the discussion into two issues and discuss their inclusions independently?

@groue
Copy link

groue commented Apr 27, 2011

Well, the two ideas do not have the same merit, and they are linked:

  • one solves two problems altogether, when the other solves only one.
  • one introduces a backwards-incompatible change

@pvande
Copy link
Contributor

pvande commented Apr 27, 2011

Issue #9 has been created for the discussion of single-line comments.

As for block comments, the best analogue I can point to is SGML / XML / HTML comments; being inherently tag-based languages, they solved the problem of nesting tags inside block comments some time ago. The distinction between the two is that HTML et al are fundamentally tags containing textual content, whereas Mustache templates are fundamentally textual content containing tags. Nesting comments inside comments is something I've never before heard a demand for, and several widely used languages abide with the lack (e.g. CSS).

I'm still 👍 to this becoming the block comment structure for v2.0.0 of the Mustache spec. Then again, I've never been upset by the balanced HTML / XML comment style (in HTML).

@kevburnsjr
Copy link

Comment Sections

Multi-line mixed content comments would be easier to implement as a reserved section tag name:

{{#!}} Don't render me, {{bro}}. {{/!}}

I just implemented this in Mustache.php in 2 lines of code.

@pvande
Copy link
Contributor

pvande commented Jun 16, 2011

To that end, {{^.}} Commented {{out}}. {{/.}} works too. Usually.

@kevburnsjr
Copy link

FYI - I created a spec and submitted a pull request.
#18

@bobthecow
Copy link
Member

This is super-old, but a question came up in #{ on IRC, so I figured I'd add a thought:

Whenever we get around to making progress on Mustache 2.0, I'd totally be on board with adding a CDATA-ish super-comment like this:

{{!! super duper {{ comment }} action going on here !!}}

{{!!
    the cool thing is, they wouldn't even have to be well formed!
    I could throw "}}" in here with reckless abandon and it's still
    all sorts of commented out :)
!!}}

Spec'd as:

/\{\{!!.*?!!\}\}/m

It's a backwards compatibility break, but the chances of a collision with IRL current templates are fairly small. This would make it easy to comment out mustache tags and such.

@groue
Copy link

groue commented Aug 1, 2013

/\{\{!!.*?!!\}\}/m
This would make it easy to comment out mustache tags and such.

Only if nesting would be supported. And /\{\{!!.*?!!\}\}/m does not support nesting.

Wrapping a template snippet in {{!! and !!}} would actually comment it out if and only if there is not another "cdata" comment in the commented snippet. In other words : you are never sure you can blindy comment out a section: you always have to check it.

So no, it is not easy.

Let's face it : the only commenting technique that deserves the "easy" qualifier, i.e. always work without requiring any Joule of the user's brain, is the end-of-line comment (#... in script langues, //... in C-like languages, why not {{!!... in Mustache).

@Krankenpfleger23
Copy link

Please close/delete all open is please Check and close the issues

@Krankenpfleger23
Copy link

Please close/delete the issues

@mustache mustache locked and limited conversation to collaborators Nov 10, 2023
@jgonggrijp jgonggrijp converted this issue into discussion #169 Nov 10, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

9 participants