stackbuilders / stache

Mustache templates for Haskell
MIT License
28 stars 10 forks source link

Render HTML lists #48

Open PaulRivier opened 4 years ago

PaulRivier commented 4 years ago

Hi,

thank you for stache. I am starting to use it for a project of mines. It is the first implementation of Mustache that I use, so please pardon me if this looks uninteresting.

Overall mustache is okay for templating. However, I am constantly facing an issue when I need to render lists of items. The pattern is always the same :

It seems that this very basic task can't be done with bare Mustache, since the following code

{{# list }} 
<h1> Here is the list </h1>
<ul>
<li> {{ item }} </li>
</ul>
{{/ list }}

would include everything in the loop.

This general issue is described here as well https://stackoverflow.com/questions/13074252/mustache-render-list-if-not-empty/19427241

Various implementations offer some enhancements to ease this common task. Is there something in stache for this situation ?

In my opinion, the issue arises from Mustache confusion of "existence test" and "loop over". Mixing the two brings this sort of issues.

I would propose a syntax extension in the form of

{{#? list }} 
<h1> Here is the list </h1>
<ul>
{{# list }}<li> {{ item }} </li> {{/ list}}
</ul>
{{/ list }}

{{#? var }} has only one semantic : render the bloc if 'var' value is a non-empty list.

What do you think ?

Regards,

mrkkrp commented 3 years ago

I believe you are looking for "inverted sections", see https://mustache.github.io/mustache.5.html