8

I understand how to transverse the data source within Handlebars but I have stumbled across a situation I cannot work out.

Using "../" you can reach the parent template scope but when iterating through the child of an object it seems to return the object and not the child.

{{#each content.items}}
{{#if prop}}
<p>{{prop}} + {{../../variable}}</p>
{{/if}}
{{/each}}

The above code snippet works fine if you iterate through an object called 'content' but as soon as you iterate through it's child, 'content.items' it no longer returns the right scope.

Here is a fiddle which demonstrates the issue. http://jsfiddle.net/sidonaldson/MDdn2/

Can anyone shed any light on what is wrong?

4

1 回答 1

2

事实证明,我原来的想法是错误的。我只在 Ember.js 的上下文中使用了 Handlebars.js。Ember 提供了一些在普通 Handlebars 中不可用的额外助手,所以这不是一个选项。但我似乎确实弄清楚了你的问题。检查这个小提琴。

<p>IN CONTENT</p>
{{#with content}}
{{#each items}}
{{#if prop}}
<p>{{prop}} + {{../../variable}}</p>
{{/if}}
{{/each}}
{{/with}}
<p>OUTSIDE CONTENT</p>
{{#each items}}
{{#if prop}}
    <p>{{prop}} + {{../../variable}}</p>
{{/if}}
{{/each}}

我不确定为什么它一开始就不起作用,但是使用with助手,然后each助手似乎起作用了。希望我已经接近你想要的。

于 2013-07-31T23:30:24.043 回答