Handlebars嵌套迭代内置帮助器 - 空输出

时间:2015-09-12 13:56:51

标签: json node.js loops helper handlebars.js

我试图在另一个中使用迭代助手。但是内在的助手并没有输出任何东西。

那是把手模板:

{{#people}}
    <h4>{{firstName}} {{lastName}}</h4>
    {{#students}}
    <h3>{{firstName}} {{lastName}}</h3>
    {{/students}}
{{/people}} 

那是我的JSON:

{
  people : [
    { firstName: "Yehuda", lastName: "Katz" },
    { firstName: "Carl", lastName: "Lerche" },
    { firstName: "Alan", lastName: "Johnson" }
  ],
  students: [
    { firstName: "Mike", lastName: "Smith" },
    { firstName: "Dan", lastName: "Knight" },
    { firstName: "Griffin", lastName: "Smith" }
  ]
}

这个问题的解决方案是什么? 在线快速尝试代码,快速点击:http://tryhandlebarsjs.com/

1 个答案:

答案 0 :(得分:0)

表达式{{#people}} ... {{/people}}会产生三种情况:

{ firstName: "Yehuda", lastName: "Katz" },
{ firstName: "Carl", lastName: "Lerche" },
{ firstName: "Alan", lastName: "Johnson" }

所以在任何这些情境中都没有学生,这就是为什么内部表达式{{#students}} ... {{/students}}没有输出。

你能详细说明你想要实现的目标吗?