3

Context

I’m trying to use Handlebars to loop through events and then nested loop through images. I need to select only the images that correspond to the event that the event loop is currently on.


Problem

I can’t pass the _id of the event inside the image nested. Is there a work-around for this? I realize I can pass variables through a helper but it would be good to know if there is a simpler way.


The following is meta-code for what is not working so far:

//attach venue image to each venue
{{#each myVenues}}
   {{#each myImages}}
      {{#if myVenues._id == myImages._id}}
         <img src="{{this.url}}>
      {{/if}}
   {{/each}}
{{/each}}

Any help would be appreciated!

Sekoul
  • 1,361
  • 2
  • 14
  • 30
  • 2
    Possible duplicate of [How can I use if condition on the meteor template?](http://stackoverflow.com/questions/28670444/how-can-i-use-if-condition-on-the-meteor-template) – Christian Fritz Dec 08 '15 at 18:59
  • 1
    meteor doesn't use handlebars anymore, it has its own template language now, called spacebars. Your question is a common one. Please see the duplicate question. – Christian Fritz Dec 08 '15 at 19:00
  • @ChristianFritz - thanks, but as I mentioned, I was already aware that I can use a helper - I'm looking for something along these lines - http://handlebarsjs.com/block_helpers.html#block-params – Sekoul Dec 08 '15 at 19:01
  • 1
    then you are still looking at the wrong documentation. You might want to read the spacebars documentation instead: https://github.com/meteor/meteor/blob/master/packages/spacebars/README.md#custom-block-helpers – Christian Fritz Dec 08 '15 at 19:15
  • Sorry, was answering your first comment before I saw the second one. All solved now, thank you for your help! – Sekoul Dec 08 '15 at 19:26

1 Answers1

3

More recent versions of spacebars supports referencing the parent. Try:

{{#each myVenues}}
    {{#each myImages}}
        {{#if ../_id == myImages._id}}
            <img src="{{this.url}}>
        {{/if}}
    {{/each}}
{{/each}}

EDIT:

Christian Fritz pointed out that your conditional logic in the if statement won't work with spacebars. If you set up a helper to evaluate the conditional logic, you can still get this working:

{{#each myVenues}}
    {{#each myImages}}
        {{ifequals ../_id myImages._id}}
            <img src="{{this.url}}>
        {{/if}}
    {{/each}}
{{/each}}

Then in a helper:

Template.registerHelper('ifequals', function(a,b) {
    return a === b;
});
Brett McLain
  • 2,000
  • 2
  • 14
  • 32
  • 1
    good luck with that, because I don't think it will work. spacebars does not natively support logic in the condition. I think you are looking for https://atmospherejs.com/ostrio/templatehelpers – Christian Fritz Dec 08 '15 at 19:13
  • 1
    You are correct, the conditional logic will fail, however the reference to the parent should work. If he already has template helpers to evaluate equivalence logic, then he can just pass those both in as an argument: {{ifequals ../_id myImages._id}} – Brett McLain Dec 08 '15 at 19:16
  • I ended up adding the ostrio package, as per Christian's suggestion - the final code is {{#if compare venueId '===' ../_id}} - works perfectly. Thank you both so much for your help, much appreciated. – Sekoul Dec 08 '15 at 19:23