【问题标题】:Handlebars nested iteration build-in helper - empty outputHandlebars 嵌套迭代内置帮助程序 - 空输出
【发布时间】:2015-09-12 13:56:50
【问题描述】:

我正在尝试在另一个中使用迭代助手。但是内部助手不输出任何东西。

这是车把模板:

{{#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/

【问题讨论】:

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


    【解决方案1】:

    表达式{{#people}}...{{/people}} 产生三个上下文:

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

    所以在任何这些上下文中都没有学生,这就是内部表达式{{#students}}...{{/students}} 没有输出的原因。

    您能否详细说明您想要实现的目标是什么?

    【讨论】:

    • 嗨,我正在做商店项目。我有 3 类男士服装。我试图在一个 div 中为每个类别添加其子类别。上面的例子如果和我的项目一样。
    【解决方案2】:

    非常感谢 J P....您的回答很棒... 正确的代码是:

    {
      people : [
        { firstName: "Yehuda", lastName: "Katz", students: [{ firstName1: "Mike", lastName1: "Smith"
    }] },
        { firstName: "Carl", lastName: "Lerche", students: [{ firstName1: "Mike", lastName1: "Smith"
    }]  },
        { firstName: "Alan", lastName: "Johnson", students: [{ firstName1: "Mike", lastName1: "Smith"
    }]  }
      ],
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多