meteormeteor-blazemeteor-helper

Meteor: Handlebars able to combine #if and #with?


Currently, I am pulling info from a third party database, and a template uses that data.

<template name="dogTypeData">
  {{#if userOwnsDog typeOfDog}}
    {{#with userOwnsDog typeOfDog}}
      // populate info here with user's dog
    {{/with}}
  {{else}}
    You do not own this kind of dog yet!
  {{/else}}
  {{/if}}
</template>

The helper sees if the owner owns this type of dog:

Template.dogTypeData.helpers({
  userOwnsDog: function(typeOfDog){
    return OwnedDogs.findOne({
      type: typeOfDog
    });
  }
});

I'm curious if there's a way to combine the #if and #with statement, or if it is even necessary to do so. For code simplicity and call userOwnsDog(typeOfDog) just once instead of twice. Like, is there an {{#ifwith}} type of statement?

Thanks!


Solution

  • You can combine them because the the initial if is unnecessary here. The same template can be rewritten like this:

    <template name="dogTypeData">
      {{#with userOwnsDog typeOfDog}}
        // populate info here with user's dog
      {{else}}
        You do not own this kind of dog yet!
      {{/with}}
    </template>
    

    Also see this section from Understanding Spacebars.