mustache / spec

The Mustache spec.
MIT License
361 stars 71 forks source link

Newline disappears after # tags, tab dissappears before # tags #111

Closed KnutRyagerInmeta closed 3 years ago

KnutRyagerInmeta commented 5 years ago

Template:


{{#imports}}
{{> import}}
{{/imports}}

import.mustache:

using {{name}};

Json:

{
  imports: [{ name: 'System.Test' }, { name: 'System.Test2' }]
}

Output:

using System.Test;using System.Test2;

I would expect 'using System.Test2;' to be on a new line. Yet this only happens if my template is:

{{#imports}}
{{> import}}

{{/imports}}

Also, a newline after {{/imports}} is removed, and tab before a {{# }} is removed. But why? They are kept with the regular {{ }} variables... any detailed documentation for this and how I can generate code with correct indentation and newlines?

Danappelxx commented 3 years ago

Hi! I believe this is an issue with the particular implementation of Mustache, rather than the spec itself. Feel free to reopen, but otherwise you should take this up with the maintainer of that particular implementation.

jgonggrijp commented 3 years ago

This can happen with an implementation that is spec-compliant (and I think this is fine). There are a couple of things in play here.

Firstly, all tags in the template are non-interpolation standalone. So they are not supposed to leave empty blank lines as traces in the output. The only nuance to this is that the lines output by the {{>import}} partial should be indented by the same amount as the partial tag, but there is no such indentation in this case.

Secondly, the import partial template doesn't end with a newline. For this reason, I would personally recommend to always end templates with a newline if they are supposed to be used as a partial. This will do what @KnutRyagerInmeta intended:

Template:

{{#imports}}
{{> import}}
{{/imports}}

import.mustache (ending with a newline):

using {{name}};

Thirdly, the mustache(5) manpage is very outdated compared to the specs. I know because I just wrote a new Mustache implementation. I initially used the manpage and then had to change quite a few things when I found out that there were specs. I think they're not mentioned on the Mustache homepage, either.