Skip to content

if-else on an array #683

@thany

Description

@thany

I'm new to mustache, so please go easy. I've found that {{#thing}} is used to loop over items in an array, and {{^thing}} is used to check if thing is falsey or an empty array. So far so good. But how do I check if an array is non-empty? I want to achieve a pretty common pattern like this:

{{if items}}
<ol>
    {{#items}}
    <li>{{.}}
    {{/items}}
</ol>
{{/if items}}
{{^items}}
  No items here
{{/items}}

The {{if items}} obviously doesn't work, but it does illustrate what I need. Given the dual function of {{#thing}} were it can also check truthyness of non-array variables afaik, if I were to use {{#items}} there, it would spew out as many lists as there are items.

Is there another notation that's not in the docs, or one that I've totally missed? I'm sure I'm not asking anything totally new, surely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions