mustache / mustache.github.com

The {{official}} website
http://mustache.github.io/
Other
2.32k stars 293 forks source link

linebreaks / empty vars in plain text files #114

Closed DaafSamson closed 11 months ago

DaafSamson commented 5 years ago

Hi, I'm using Mustache to merge plain text templates. I have the following scenario:

data

{
"name":"James"
,"colour":"green"
,"book":"War and peace"
}

template

This is what we know about {{name}}: 
{{#colour}}- Favourite colour is {{colour}}{{/colour}}
{{#fruit}}- Favourite fruit is {{fruit}}{{/fruit}}
{{#book}}- Favourite book is {{book}}{{/book}}

Result:

This is what we know about James: 
- Favourite colour is green

- Favourite book is War and peace

Note the glaring gap between the two lines... Now, in a HTML template this would not be and issue, white spaces and empty lines are eaten by the browser and the result flows nicely. In a plain text environment though, this is an issue.

Is there an established way to deal with this? Is there a construct that drops the line including the line break after the field?

DaafSamson commented 5 years ago

So, I'm probably doing things not needed at all and just missing the point. But I needed a solution and this is what I did.

  1. I'm working in powershell (hey, it's an environment). Since there was no project available that met my criteria, I created one: https://github.com/Palloquin/PSStubble/. It's based on Stubble.

  2. I added an option to do custom white-space replacement. To achieve this I added my own extra tags to to Mustache standard: {| and |}

  3. I use these two regular expressions to clean up after doing a standard (stubble based) Mustache merge: {\|\s*\|}((\r\n)|\r|\n)? --> remove block sstarting and ending with my tags, with nothing but white-space between them, include a potential line-break after the tag ({\|)|(\|}) --> remove leftover tags I simply replace with empty strings

This would result in something like this:

New template:

This is what we know about {{name}}: 
{|{{#colour}}- Favourite colour is {{colour}}{{/colour}}|}
{|{{#fruit}}- Favourite fruit is {{fruit}}{{/fruit}}|}
{|{{#book}}- Favourite book is {{book}}{{/book}}|}

Output

This is what we know about James: 
- Favourite colour is green
- Favourite book is War and peace

This is a quick and dirty hack, I'm sure I'm missing some edge cases, and could do better. I might not even need to do this at all, but heck, it works. I'm very open to comments

vanta commented 3 years ago

Any update here? Noone with the same problem?

jobol commented 3 years ago

I had to change mustach to fix a compatibility with the spec (see https://gitlab.com/jobol/mustach/-/issues/30)

Spec is: a line with a standalone tag is not emitted.

So writing your template as below should make the job

This is what we know about {{name}}: 
{{#colour}}
- Favourite colour is {{colour}}
{{/colour}}
{{#fruit}}
- Favourite fruit is {{fruit}}
{{/fruit}}
{{#book}}
- Favourite book is {{book}}
{{/book}}

I removed the {| |} so this is perhaps not a valuable solution in that case ....

jgonggrijp commented 11 months ago

The above solution is correct. Closing this, as this repository is only about the Mustache website, not about the template language.