For each loop in Blaze template pass data context to child template

Hello everyone,

I am using a for each loop in my Template. Within that loop I want to pass the element specific data to a child template. However, when accessing the passed data from one of the child templates, they all have the same data (and not e.g. the elements respective name). get_elements and get_other_elements both return fetched MongoDB documents. The name of the element is displayed correctly.

{{#each get_elements}}
    <li>
       <h3 class="level-3 rectangle">
          {{name}}{{> childTemplate elemID=_id elemName=name}}
       </h3>
       <ol class="level-4-wrapper">
          {{#each get_other_elements _id}}
            <li>
              <h4 class="level-4 rectangle">
                {{name}}
                {{ > anotherChildTemplate data=this}}
              </h4>
            </li>
          {{/each}}
       </ol>
    </li>
{{/each}}

What am I doing wrong?

Any help/hint is highly appreciated. Thank you!

Hello,
You should use each … in …

https://www.blazejs.org/guide/spacebars#Each-in

Thank you for your answer.

Unfortunately that doesn’t solve my problem. I still face the issue that the value I access from the child template equals the value of the first list element of the parent template.

I suspect the problem to be related to the fact that all child template widgets have the same ID and therefore the values of the first created template is always used, but that is just guessing.

To give a little bit more context, my child template is of the following structure:

<template name="childTemplate">

  <img id="open_child_popup" src="/img/..." >
...

My event listener function where I want to access the data looks like the following:

'click #child_template_submit'(event, template){
    const id = template.data.elemID;
...

The “problem” was indeed related to all child templates having the same ID. I added an unique ID to all elements and adapted the event handlers respectively. That solved it.