流星子模板可以访问父模板助手吗? [英] Can Meteor child templates access parent template helpers?

查看:89
本文介绍了流星子模板可以访问父模板助手吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

< template name =child>
{{#if show}}
//做某事
{{/ if}}
< / template>

如果我们将'show'分配给父模板:

  if(Meteor.isClient){
Template.parent.show = function(){
return Session.get('isShowing');


$ / code $ / pre

子模板有任何方式可以访问 >你可以创建一个通用的handlebars助手,这样你就可以在html中的任何地方使用Sessions值:
$ b 客户端js

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

客户端HTML

 < template name =child> 
{{#if sessionshow}}
//做某事
{{/ if}}
< / template>

同样,您也可以使用 {{sessionshow}} / code> / {{#if sessionshow}} ,而不必使用 Template.parent。再次显示助手。



关于使用 ../ 表示法。有些情况下可能无效: https://github.com/meteor/meteor/issues / 563 。基本上它可以在{{#block helpers}}中使用,但不能与模板一起使用,但它可以在块助手中使用,如果它包含子模板的话。

 < template name =child> 
{{#if ../show}}
做某事
{{/ if}}
< / template>


Say we have a parent template and a child template:

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

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

If we assign 'show' to the parent template:

if (Meteor.isClient){
   Template.parent.show = function(){
     return Session.get('isShowing');
   }
}

Is there any way for the child template to have access to it?

解决方案

Edit

You could make a universal handlebars helper so you could use Sessions values anywhere in your html:

Client js

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

Client HTML

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

Similarly, you could also use {{session "show"}} / {{#if session "show"}} in your parent template and not have to use the Template.parent.show helper anymore.

Regarding the use of ../ notation. There are certain scenarios it may not work: https://github.com/meteor/meteor/issues/563. Basically it works within {{#block helpers}} but not with templates, but it would work in a block helper if it contains a subtemplate.

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

这篇关于流星子模板可以访问父模板助手吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆