如何在不循环 Mustache JS 的情况下检查数组?

How to check if array without looping Mustache JS?

有没有一种不用循环就可以检查数组是否存在的方法?我发现使用 {{#foos}}{{/foos}} 循环遍历数组。不过,我需要先检查数据是否存在。

像这样:

{{if foos}}
    <ul>
        {{#foos}}
            <li>{{foo}}</li>
        {{/foos}}
    </ul>
{{/if}}
{{^foos}}
    No foos!
{{/foos}}

你在追求 {{#foos.length}}

var template = `
{{#foos.length}}
  <ul>
    {{#foos}}
      <li>{{foo}}</li>
    {{/foos}}
  </ul>
{{/foos.length}}
{{^foos.length}}
    <p>No foos!</p>
{{/foos.length}}`

const div = document.getElementById("result")

// with content
div.insertAdjacentHTML("beforeend", Mustache.render(template, {
  foos: [{
    foo: 'Alpha'
  }, {
    foo: "Beta"
  }, {
    foo: "Gamma"
  }]
}));

// without
div.insertAdjacentHTML("beforeend", Mustache.render(template, {
  foos: []
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.1/mustache.min.js"></script>

<div id="result"></div>