handlebars.js'其他'没有按预期工作

时间:2013-06-13 11:59:07

标签: jquery handlebars.js

这里有

http://jsfiddle.net/3U4uh/28/个工作代码,我如何添加'if'帮助程序来检查里面是否有数据?

我试过这样做:

      <script id="shoe-template" type="x-handlebars-template">
                {{#if people}}
                    <li class="shoes">
                        <p>{{name}}</p>
                    </li>
                {{/if}}
  </script>


但它不起作用

1 个答案:

答案 0 :(得分:2)

如果您要循环浏览某些内容并在列表为空时显示“没有任何内容”消息,请使用{{#each}}{{else}}

{{#each array}}
    <li class="shoes">
        <p><b>{{name}}</b></p>
    </li>
{{else}}
    <li>
        <p><b>Nothing there</b></p>
    </li>
{{/each}}

如果array为空或根本没有定义,那么同样有效。

如果你只想测试一个数组是否存在并包含某些内容,那么一个简单的{{#if}}应该可以解决这个问题:

{{#if array}}
    The array has things in it.
{{else}}
    The array is empty or not defined at all.
{{/if}}

演示:http://jsfiddle.net/ambiguous/ZmXzN/