user3475602
user3475602

Reputation: 1217

Access parent data context in Template rendered function in Meteor

I have the following parent template:

<template name="parentTempl">
    {{#each child}}
       {{> childTempl}}
    {{/each}}
</template>

I want to access the parent data context in childTempl:

Template.childTempl.rendered = function() {
    console.log(this.parent.data); // ?
};

How can I do this? Any help would be greatly appreciated.

Upvotes: 14

Views: 11734

Answers (1)

mark
mark

Reputation: 1725

You can use Template.parentData(n) to access the parent context inside any template helper or rendered callback. See the docs here. Internally, all it does is call the Blaze getView method for the parent view until it hits the desired parent context (as defined by n).

Upvotes: 23

Related Questions