简体   繁体   English

Handlebars.js中的多个循环失败

[英]Multiple loops fail in Handlebars.js

In my Handlebars template, I am trying to loop over the same data twice, but it fails on the second loop. 在我的Handlebars模板中,我尝试对相同的数据进行两次循环,但是在第二次循环中失败。 This is how my template looks: 这是我的模板的外观:

<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>

And this is the JavaScript: 这是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);

However, it does not render anything for the second loop: 但是,它不会为第二个循环渲染任何内容:

First loop:
John Doe
Jane Doe
Second loop:

I created a jsFiddle for this here: http://jsfiddle.net/G83Pk/ and have even logged this in as a bug https://github.com/wycats/handlebars.js/issues/408 . 我为此在这里创建了一个jsFiddle: http : //jsfiddle.net/G83Pk/ ,甚至还以bug https://github.com/wycats/handlebars.js/issues/408登录。 Does anyone know how to fix this or know what the problem is? 有人知道如何解决此问题,还是知道问题出在哪里?

As far as I know, the correct way to iterate over an array is through a each block helper 据我所知,迭代数组的正确方法是通过each块帮助器

Your template would be written as 您的模板将写为

<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>

An updated Fiddle http://jsfiddle.net/nikoshr/G83Pk/1/ 更新的小提琴http://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>

You need to correct your iterator to use the each block helper. 您需要更正迭代器才能使用每个块帮助器。 And your context variable is invalid input for the each iterator. 而且您的上下文变量对于每个迭代器都是无效的输入。 The above code is the proper way to do what you want. 上面的代码是执行所需操作的正确方法。

Not sure with the comments, but I encountered very strange behavior while having similar kind of scenario in my code. 不确定注释,但是在我的代码中有类似情况时遇到了非常奇怪的行为。

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

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

{{/with}}

It worked with first loop but didnt worked with second loop. 它适用于第一个循环,但不适用于第二个循环。 I did all scenarios at the ended with some strange solution. 最后,我用一些奇怪的解决方案完成了所有方案。 If I add any Html script or comment at the end of {{#each models}} of second loop, then second loop regains its context and display values properly. 如果在第二个循环的{{#each models}}的末尾添加了任何HTML脚本或注释,则第二个循环将重新获得其上下文并正确显示值。

So this worked flawlessly. 因此,它可以完美地工作。

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

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

{{/with}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM