handlebars.js “each” loop inside another “each” loop 3

…衆ロ難τιáo~ 提交于 2019-12-31 08:57:54

问题


Suppose I want to build a dynamic table. How do I run each inside each. If the only varible that represents current item is this.

   {{#each by_width}}
       {{#each by_height}}
          {{this}} // how do refer to this from the outer loop?
       {{/each}}
   {{/each}}

回答1:


You can use ../ to access the parent in a Handlebars template:

{{#each by_width}}
    {{#each by_height}}
       w: {{../this}}
       h: {{this}}
    {{/each}}
{{/each}}

That of course assumes that by_height is inside each element of by_width, if they're both at the top level then you'd need another ../:

{{#each by_width}}
    {{#each ../by_height}}
       w: {{../this}}
       h: {{this}}
    {{/each}}
{{/each}}

Demo: http://jsfiddle.net/ambiguous/PNTXw/




回答2:


Don't write {{../this}} but {{..this}}.



来源:https://stackoverflow.com/questions/14059157/handlebars-js-each-loop-inside-another-each-loop-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!