Skip to content
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

Render HTML lists #48

Open
PaulRivier opened this issue May 12, 2020 · 1 comment
Open

Render HTML lists #48

PaulRivier opened this issue May 12, 2020 · 1 comment
Labels

Comments

@PaulRivier
Copy link

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 :

  • when the list is not empty
    • insert list title
    • open list tag
    • loop over list items
    • close list tag
  • when the list is empty, render nothing

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
Copy link
Collaborator

mrkkrp commented Oct 13, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants