Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Meteor child templates access parent template helpers?

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?

like image 322
J2K Avatar asked Feb 28 '13 03:02

J2K


2 Answers

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>
like image 146
Tarang Avatar answered Nov 08 '22 01:11

Tarang


You can also register a common helper :

Template.registerHelper('isTrue', function(boolean) {
    return boolean == "true";
});

And call it just like that in your html:

<input type="checkbox" checked="{{isTrue attr}}"/>
like image 27
Ser Avatar answered Nov 07 '22 23:11

Ser