Handlebars.js中有多个循环失败

Multiple loops fail in Handlebars.js

本文关键字:循环 失败 js Handlebars      更新时间:2023-09-26

在我的Handlebars模板中,我试图对相同的数据进行两次循环,但在第二次循环时失败了。我的模板是这样的:

<div id="placeholder"></div>
<script type="text/x-handlebars" id="people-template">
  First loop:<br />
  <ul>
    {{#.}}
        <li>{{name}}</li>
    {{/.}}
  </ul>
  Second loop:<br />
  <ul>
    {{#.}}
        <li>{{name}}</li>
    {{/.}}
  </ul>
</script>

这就是JavaScript:

var context= [
  { name: "John Doe", location: { city: "Chicago" } },
  { name: "Jane Doe", location: { city: "New York"}  }
];
var template = Handlebars.compile($("#people-template").text());
var html = template(context);
$('#placeholder').html(html);

然而,它不会为第二个循环呈现任何内容:

First loop:
John Doe
Jane Doe
Second loop:

我在这里创建了一个jsFiddle:http://jsfiddle.net/G83Pk/甚至已经将其作为bug登录https://github.com/wycats/handlebars.js/issues/408.有人知道如何解决这个问题吗?或者知道问题出在哪里?

据我所知,迭代数组的正确方法是通过each块助手

你的模板将被写为

<script type="text/x-handlebars" id="people-template">
  First loop:<br />
  <ul>
    {{#each .}}
        <li>{{name}}</li>
    {{/each}}
  </ul>
  Second loop:<br />
  <ul>
    {{#each .}}
        <li>{{name}}</li>
    {{/each}}
  </ul>
</script>

更新的Fiddlehttp://jsfiddle.net/nikoshr/G83Pk/1/

<div id="placeholder"></div>    
<script id="people-template" type="text/x-handlebars">
  First loop:<br />
  <ul>
    {{#each context}}
        <li>{{name}}</li>
    {{/each}}
  </ul>
  Second loop:<br />
  <ul>
    {{#each context}}
        <li>{{name}}</li>
    {{/each}}
  </ul>
</script>
<script type="text/javascript">
var template = Handlebars.compile($("#people-template").html());
var json = {
    "context": [
        { "name": "John Doe", "location": { "city": "Chicago" } },
        { "name": "Jane Doe", "location": { "city": "New York"} }
    ]
};
var html = template(json);
$('#placeholder').html(html);
</script>

您需要更正迭代器以使用每个块帮助器。并且您的上下文变量对于每个迭代器都是无效的输入。上面的代码是执行您想要的操作的正确方法。

对注释不确定,但我在代码中遇到类似场景时遇到了非常奇怪的行为。

{{#with xxxxx}}
                               {{#each models}}
                                {{#with attributes}}
                                {{value}}                   ---- Worked here
                                {{/with}}
                                {{/each}}
                                {{#each models}}
                                {{#with attributes}}
                                {{value}}                   ---- Didn't Worked here
                                {{/with}}
                                {{/each}}
{{/with}}

它适用于第一个循环,但不适用于第二个循环。最后,我用一些奇怪的解决方案完成了所有场景。如果我在第二个循环的{{#each-models}}}末尾添加任何Html脚本或注释,那么第二个环路将重新获得其上下文并正确显示值。

所以这是完美的。

{{#with xxxxx}}
                               {{#each models}}
                                {{#with attributes}}
                                {{value}}                   ---- Worked here
                                {{/with}}
                                {{/each}}
                                {{#each models}}   {{! Comment added }}
                                {{#with attributes}}
                                {{value}}                   ---- It works now.
                                {{/with}}
                                {{/each}}
{{/with}}