Skip to content

Render HTML lists #48

@PaulRivier

Description

@PaulRivier

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,

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions