Remove linebreaks when using {{each}}

38 Views Asked by At

I have the following handlebars template to generate a changelog.md with gitmoji:

#  {{nextRelease.version}}

{{#with commits}}
{{#if sparkles}}
## ✨ New Features
{{#each sparkles}}
- {{> commitTemplate}}
{{/each}}
{{/if}}
{{/with}}

when the changelog is generated I get results like this (in markdown):

#  19.15.0

## ✨ New Features
-  first feature commit

-  second feature commit

-  third feature commit

while this visually does not affect the markdown result, I feel that the readability can be a little messy or atleast maybe this is just personal preference, but I would like to get the following result (remove the linebreaks between each feature):

#  19.15.0

## ✨ New Features
-  first feature commit
-  second feature commit
-  third feature commit

I had been searching and found about this documentation: https://handlebarsjs.com/guide/expressions.html#whitespace-control

So I tried like this:

{{~#each sparkles}}
- {{> commitTemplate}}
{{~/each}}

but this gave me the following wrong result:

#  19.15.0

## ✨ New Features-  first feature commit
-  second feature commit
-  third feature commit

Also other ways I tried but did not work were:

## ✨ New Features<br>
{{~#each sparkles}}
- {{> commitTemplate}}
{{~/each}}

and:

## ✨ New Features

{{~#each sparkles}}
- {{> commitTemplate}}
{{~/each}}
1

There are 1 best solutions below

0
romellem On BEST ANSWER

White-space control is how you should be handling this.

Note that you can include the ~ character on either the left or right side of your handlebars expression and they don't have to be "balanced" at all. You can include a ~ in a closing expression and not its opening.

It is a little hard to replicate since it isn't entirely clear what your commitTemplate partial looks like, but my guess is you only need to remove whitespace before the closing {{/each}}, so your full code would look like

#  {{nextRelease.version}}

{{#with commits}}
{{#if sparkles}}
## ✨ New Features
{{#each sparkles}}
- {{> commitTemplate}}
{{~/each}}
{{/if}}
{{/with}}

Handlebars playground link.