Passing a context variable through an #each block


#1

I’ve got a nested #each loop and the inner loop needs some info from the outer loop. To best explain, here’s the working code (using Meteor v.NEXT from github):

{{#each filteredCollection}} //returns simple array like ["client", "vendor"]
  {{#with idx=@index heading=this}}
    {{#if filteredItems}} ///needs the @index in its helper function
      {{#let heading=heading}}
        {{#each filteredItems}}
          {{#with doc=this heading=heading}}
            {{>filteredItem}} //needs heading & the document
          {{/with}}
        {{/each}}
      {{/let}}
    {{/if}}
  {{/with}}
{{/each}}

As shown, this works perfectly. I needed to use a #let to permeate the #each block, and then a #with once inside to give it context. This seems really hacky to me, is there a better way to do it?


#2

i had to solve this too. you can use {{ … }} and {{ . }} to refer to the parent and current contexts, respectively. also meteor provides a way to get parent contexts in the template api.


#3

Oh man, time to call it a night, you’re absolutely right I forgot about that. Got rid of the let & used .. in the second #with. Still a little ugly, but it better than before. Cheers!