1

How can I create simple conditional handlebar?

What I want to do is get from collections that if mood =='+', render the roll in white, else render some other color.

This is HTML:

{{#if moodIsPlus}}
     <tr>
        <td>
            <strong>{{mood}} <i class="icon-hand-up" value="+"></i></strong> {{message}}<br>
         </td>
    </tr>
    {{else}}
    <tr class="info">
        <td>
            <strong>{{mood}} </strong> {{message}}<br>
         </td>
    </tr>
    {{/if }}

which moodIsPlus is define in JS:

Template.messages.moodIsPlus = function() {
  return Messages.find({mood:'+'});
}

and it returns everything.

By the way, are handlebars and handlebar.js the same thing? Where can I get more reference on handlebar in meteorjs?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
sooon
  • 4,718
  • 8
  • 63
  • 116
  • 1
    Yes, Handlebars is same as Handlebar.js. Meteor By default uses the Handlebar as templating system. Whatever is on [site] (handlebarsjs.com) applied same to Meteor env. – ajduke Mar 02 '14 at 16:56
  • possible duplicate of [Handlebars.js if block helper ==](http://stackoverflow.com/questions/15088215/handlebars-js-if-block-helper) – ajduke Mar 02 '14 at 16:59

2 Answers2

1

Checkout the Meteor Smart Package Handlebar-helpers as it comes with lots of similar helpers.

Here is how you would achieve what you are looking for with it:

<tr>
    <td>
        <strong>{{#if $eq mood '+'}}<i class="icon-hand-up" value="+"></i>{{/if}}</strong> {{message}}<br>
    </td>
</tr>
1321941
  • 2,139
  • 5
  • 26
  • 49
1

You can used spacebar or handlebar(old) function to accomplish this.

First is get all messages. You can put it inside Template helpers like this:

Messages.helpers({
 messages: function(){
   return Messages.find({});
 },
 moodIsPlus: function(mood){
   return (mood === "+") ? true : false; // return true if mood === "+"
 }
});

Then, in your template.

 {{#if moodIsPlus mood}} mood is from your Messages collection. that contains "+" or whatever.
   <tr>
    <td>
        <strong>{{mood}} <i class="icon-hand-up" value="+"></i></strong> {{message}}<br>
     </td>
</tr>
{{else}}
<tr class="info">
    <td>
        <strong>{{mood}} </strong> {{message}}<br>
     </td>
</tr>
{{/if }}

Hope this help.

Edion Larosa
  • 114
  • 1
  • 1
  • 8