torayeff
torayeff

Reputation: 9702

Accessing parent data context in meteor templates

How can I access parent template's data in Meteor. In below example I want to access <template name="post"> template data from <template name="photoGallery"> This is what I am doing but it does not work:

<template name="post">
  <div class="well">

    <p class="text-left text-muted"><span class="glyphicon glyphicon-time"></span> <i>{{post.date}}</i></p>

    <div class="post-text">
      <p>{{post.text}}</p>       
    </div>

    <div class="photo-gallery">
      {{> photoGallery}}
    </div>

    <hr>
    <div class="like-post text-right">
      <button>        
        <span class="glyphicon glyphicon-heart liked"></span>
        {{post.likeCount}}
      </button>      
    </div>

  </div>
</template>

and:

<template name="photoGallery">
  {{#each post.photos}}  
    <img src="{{src}}"> //this does not work
  {{/each}}
</template>

This is my js file for post template:

Template.post.helpers({  


    post: {
        text: 'Teks, surat, audio we video. Postda tekst bolanda test uchin ulanyljak yazgy. Shu yazgynyn maksimum simwol sany yuz kyrk simwoldan gechmeli dal. Eger gechen yagdayynda onda kop nokatlar goyulmaly.',
        date: '31-12-205 23:59',
        likeCount: 13,
        photos: [      
          {
            src: 'http://cs625423.vk.me/v625423655/899b/W9agrD2UimQ.jpg',
            src_small: 'http://cs625423.vk.me/v625423655/899a/zkipjxINKM8.jpg',
            src_big: 'http://cs625423.vk.me/v625423655/899c/uL2EOORDYV4.jpg'        
          },
          {
            src: 'http://cs625423.vk.me/v625423655/899b/W9agrD2UimQ.jpg',
            src_small: 'http://cs625423.vk.me/v625423655/899c/uL2EOORDYV4.jpg',
            src_big: 'http://cs625423.vk.me/v625423655/899a/zkipjxINKM8.jpg'
          },
          {
            src: 'http://cs623220.vk.me/v623220655/1e3be/uj7M9HwtkbA.jpg',
            src_small: 'http://cs623220.vk.me/v623220655/1e3bd/hkoSv-zpb4Y.jpg',
            src_big: 'http://cs623220.vk.me/v623220655/1e3bf/Zb3sQa0aX0U.jpg'
          },
          {
            src: 'http://cs623216.vk.me/v623216655/16c18/3mXm6-EZ-aM.jpg',
            src_small: 'http://cs623216.vk.me/v623216655/16c17/RtKyVS-D7GA.jpg',
            src_big: 'http://cs623216.vk.me/v623216655/16c19/_jy0aZu2GEA.jpg'
          }
        ],
    ...

Upvotes: 1

Views: 1532

Answers (4)

Tomas Hromnik
Tomas Hromnik

Reputation: 2200

You can access parent data with ../

<template name="photoGallery"> {{#each ../post.photos}} <img src="{{src}}"> {{/each}} </template>

If it does not work, you can try:

<div class="photo-gallery">
  {{#with post}}
    {{> photoGallery}}
  {{/with}}
</div>

and your photo gallery template will look like:

<template name="photoGallery"> {{#each this.photos}} <img src="{{src}}"> {{/each}} </template>

If you use #with helper, photoGallery template will get post context and you can use post attributes via this.

Upvotes: 0

David Weldon
David Weldon

Reputation: 64312

The desired outcome is for the child template to access a result of the parent's helper. You can do this by setting the context of the photoGallery template like so:

{{> photoGallery post}}

Now, inside of photoGallery, the context is a post so we can iterate over the photos:

<template name="photoGallery">
  {{#each photos}}
    <img src="{{src}}">
  {{/each}}
</template>

Upvotes: 2

torayeff
torayeff

Reputation: 9702

This is how I managed to solve my problem. I just passed post.photos variable to child template. In <template name="post">:

...
<div class="photo-gallery">
  {{> photoGallery photos=post.photos}}
</div>
...

And in <template name="photoGallery">:

{{#each photos}}
  <img src="{{src}}">
{{/each}}

Upvotes: 0

PJC
PJC

Reputation: 997

I think parentData is what you are after.

Upvotes: 0

Related Questions