Using if-else conditions in Blaze

I am trying to create a system with conditional statements. I am saving multiple entries in a collection. In one field of that collection, I am saving 3 values: 1,2 and 3.

Now i want to display a picture system in blaze but i am not sure, how to do it.
What i wanna do, is get result from db first . then on basis of that i want to display set of pics. like for example if result is 1, i want to show pic of tiger. if value is 2, i want to show cat or if its there i want to show dog. basically i am not sure about how to do it.
i was coding in PHP before so never have to worry about conditional statements.but now blaze is giving me hard time to do basic things.

Thanks in advance for those, who are going to put any efforts.

Instead of having the logic in the template with nested {{#if}}{{else}}{{/if}} I would provide the logic via a helper

<template name="animals">
 {{#if subscriptionsReady}}
  {{#each animals}}
   <img src="{{showAnimalHelper}}">
  {{/each}}
 {{else}}
   Loading ...
 {{/if}
</template>

Now the code

//
// let's say you publish the animal(s) from the animals collection based on the logged in user
//
Template.animals.onCreated(function(){
 var instance = this;
 instance.subscribe('animals');
});
Template.animals.helpers({
 animals(){
  return Animals.find();
 },
 showAnimalHelper(){
  var animal = this;
  var animalImage = '/invalid_animal.png';
  if(animal) {
   switch(animal.type) {
     case 1: animalImage = '/tiger.png';break;
     case 2: animalImage = '/cat.png';break;
     default: animalImage = '/dog.png';break;
   }
  }
  return animalImage;
 }
});

Haven’t tested it, but it should work

2 Likes

Good way to ask for help on these forums.

i am really helpless whether i should say something for your comment or not. may be i should stay quiet. you look like a huge fan of meteor documentation. personally i find them incomplete referring to other places and lacking basic information.

I think it’s absolutely fine to say you think they’re incomplete. However, to use a pejorative term like shitty is sending a clear message to the maintainers of Blaze - and they are Meteor community members, not MDG.

I think it’s good enough right now, and getting better all the time. Much of that “getting better” is down to contributions from the community.

4 Likes

I apologise. i should not use such harsh language for anything. I respect the hard work, People doing to maintain documentation and improving. I never intend to offend anyone specially people helping others.
thanks for your help and sorry to everyone.

3 Likes

i think the doc is very good but it is not easy to get you started (maybe also not intended).
i really liked http://meteortips.com/first-meteor-tutorial/ to get started. i also skipped some parts of it initially then came back to it. now i feel that i am able to work with the offical doc properly.

1 Like

This seems like an apparent limitation of Blaze to newcomers, as it did myself: The built in if block helper will only check a boolean result, and not additional conditionals.

This is why I always have a series of helpers I use to do beyond simple bolean comparison of values for conditional rendering in the DOM.

The built in block helper gets you from the limited:

{{#if someBool}}{{/if}}

To get the fuller functionality of an additional comparison operator helper, the first parameter passed to the ‘if’ block helper can itself be a helper, with additional parameters listed being passed to that second helper.

You can see some helpers I’ve made for this.

As such now this is possible:

{{#if eq value 1}}{{/if}}

Of course you can go further with nesting of helpers:

{{#if OR (gte value 1) (eq state "active")}}
{{/if}}

Philosophically Blaze doesnt give you this ability out of the box because you can easily pollute your template code with logic better implemented in template methods/helpers.

2 Likes