You need to make a blaze / handlebars helper for this. If statements in blaze do not have this capability out of the box. You need to write some code for it. Its very simple though.
Blaze / Handlebars Helper: (Not sure why you need a switch in this case)
Ok, so I’m missing something still I guess. What I’m trying to do is display an image in a table column based upon a siteStatus field in a collection. So if the siteStatus is “1”, display a green image, if it’s “2” display yellow image and so on.
I have this helper:
Template.registerHelper('statusHelper', function(siteStatus) {
switch(siteStatus) {
case 1:
return true;
break;
case 2:
return true;
break;
case 3:
return true;
break;
}
well, i would probably transform that collection to make it easier for me, but that is not answer to be given on forums without additional xy posts discussion how to do it in that exact case
As we’re already in “what I would do” mode, personally I’d keep <img> in a subtemplate and make the helper for this particular subtemplate instead of global one.
I copied and pasted his code into my project and it’s not working correctly for me. It’s not giving an error. The issue is that it’s not returning a valid image (the color portion of the img tag is not getting returned by the helper. Hence I only get: images/circle-.png
I’m looping through a collection using {{#each sites}}
So it seems that the siteStatus value is not getting adding to the img tag, leaving the tag as images/circle-.png instead of images/circle-green.png, images/circle-yellow.png or images/circle-red.png.
Wow, I’m really sorry, but I’m just not getting it. I literally copied and pasted your exact code into my JS and HTML, and it’s giving the same results each time. Would it be because I added the HTML to a template instead of the body?
I’m still having the hardest time with this. I gave up on it for a bit, but now I’m coming back to it and hope you can point me towards what I’m doing wrong.
I have the following in my JS file:
Template.registerHelper('pickColor', function(inputNumber) {
switch(inputNumber) {
case 1:
return "green";
break;
case 2:
return "yellow";
break;
case 3:
return "red";
break;
}
});