Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor子模板可以访问父模板帮助程序吗?_Meteor_Handlebars.js - Fatal编程技术网

Meteor子模板可以访问父模板帮助程序吗?

Meteor子模板可以访问父模板帮助程序吗?,meteor,handlebars.js,Meteor,Handlebars.js,假设我们有一个父模板和一个子模板: <template name="parent"> {{> child }} </template> <template name="child"> {{#if show}} //Do something {{/if}} </template> if (Meteor.isClient){ Template.parent.show = function(){ return

假设我们有一个父模板和一个子模板:

<template name="parent">
  {{> child }}
</template>

<template name="child">
  {{#if show}}
    //Do something
  {{/if}}
</template>
if (Meteor.isClient){
   Template.parent.show = function(){
     return Session.get('isShowing');
   }
}
子模板是否有任何方式可以访问它?

编辑

您可以制作一个通用把手帮助器,以便可以在html中的任何位置使用会话值:

客户端js

Handlebars.registerHelper('session', function(key) {
    return Session.get(key);
});
客户端HTML

<template name="child">
  {{#if session "show"}}
    //Do something
  {{/if}}
</template>

您还可以注册一个常用帮助器:

Template.registerHelper('isTrue', function(boolean) {
    return boolean == "true";
});
并在html中这样称呼它:

<input type="checkbox" checked="{{isTrue attr}}"/>


我想说这更多的是一个车把问题,车把支持“../”,但我不确定它会回到多久以前,我在车把文档中看到了这一点,但是你能在Meteor中重现这一点吗?当我尝试时,我在控制台中得到一个错误:“未捕获错误:太多的“..”段”。据我所知,../引用了我们正在处理的数据的父上下文,因此如果我们有:context={name:John,posts:{post1:body1,post2:body2}},并且我们在posts属性上循环,我们可以使用../来访问name属性。但是我不认为我们可以使用../来访问我们尚未访问的全新数据对象,或者至少我无法让它工作。是的,你是对的,看起来
。/
符号不一定总是与子模板一起工作。我用另一种解决方案更新了答案谢谢你的答案。这不完全是我所希望的(如果../show有效的话,它将是完美的),但我认为这是一个有用且相当干净的解决方法。奇怪的是,人们可以在把手中访问父上下文,但不能在helpers/以编程方式访问。你知道这是否已经改变了吗?
<input type="checkbox" checked="{{isTrue attr}}"/>