把手 - 是否可以部分访问父上下文? [英] handlebars - is it possible to access parent context in a partial?
问题描述
我有一个车把模板,可以为子元素加载部分.
I've got a handlebar template that loads a partial for a sub-element.
我需要从调用模板中的父上下文访问一个变量,从部分内部...
似乎无法解析部分内部的任何内容.
I would need to access a variable from the parent context in the calling template, from within the partial. ..
doesn't seem to resolve to anything inside the partial.
简化代码如下:
模板
{{#each items}}
{{> item-template}}
{{/each}}
部分
value is {{value}}
(显然真正的代码更复杂,但原理是一样的,在部分..
内似乎是未定义的.)
(obviously the real code is more complicated but it's the same principle, within the partial ..
appears to be undefined.)
为了显示它是未定义的,我使用了一个非常简单的帮助器whatis
,如下所示:
To show it's undefined, I've used a very simple helper whatis
like this:
Handlebars.registerHelper('whatis', function(param) {
console.log(param);
});
并将上面的代码更新为:
and updated the above code to this:
更新模板
{{#each items}}
{{whatis ..}} <-- Console shows the correct parent context
{{> item-template}}
{{/each}}
更新部分
{{whatis ..}} <-- Console shows "undefined"
value is {{value}}
有没有办法解决这个问题?我错过了什么吗?
Is there a way to go around that issue? Am I missing something?
推荐答案
以防万一有人偶然发现这个问题.此功能现在存在于 Handlebars 中.
Just in case anyone stumbles across this question. This functionality exists now in Handlebars.
这样做:
{{#each items}}
{{! Will pass the current item in items to your partial }}
{{> item-template this}}
{{/each}}
这篇关于把手 - 是否可以部分访问父上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!