Reputation: 3174
So if I have a template:
<template name="myTemplate">
{{foo}}
</template>
and the template helper:
Template.myTemplate.foo = function() {
blah = Session.get('blah');
// code to do stuff with blah
return blah;
};
and then I have another template:
<template name="myOtherTemplate">
{{foo}}
</template>
and I want the data context of this template to be the same as the previous template what would I do?
It first occurred to me that using {{#with}} might be the right approach but that seems like it would only work if the scope of the second template was already inside the first.
Ultimately I would like to be able to use all helpers defined for one template within another template and know how to do that.
Upvotes: 2
Views: 2507
Reputation: 36900
Seems like you are asking one of two questions:
If you are using myOtherTemplate
inside myTemplate
, the context of the other template will be the same as this one, unless you explicitly pass it something else as the second argument of the partial.
<template name="myTemplate">
{{> myOtherTemplate foo}}
</template>
If you want to use a helper across more than one template, declare it in a global helper. This will make {{foo}}
available in all templates:
Handlebars.registerHelper("foo", function() {
blah = Session.get('blah');
// code to do stuff with blah
return blah;
});
If you want to make your own data context on the fly (this is rare), do the following:
<template name="myTemplate">
{{{customRender}}}
</template>
Template.myTemplate.customRender = function() {
return Template.otherTemplate({
foo: something,
bar: somethingElse,
foobar: Template.myTemplate.foo // Pass in the helper with a different name
});
};
This object is basically what Iron-Router will pass to your template on render.
Note that you wiill need to use triple handlebars {{{ }}}
or use new Handlebars.SafeString
to tell it not to escape the template.
Upvotes: 3