Template if condition


#1

Hello,

How can I use simple if statement inside the template ? I want to check for greater than or equal to. Assuming that getdata method returns some value.

{{#if getdata > 1}}

<p>Greater than 1</p>

{{else}}

<p>Less than 1 or 1</p>

{{/if}}

This ain’t working. Any help ?


#2

You can’t enter such statements in your templates but need just a helper for that like

template.mytemplate.helpers({
  check_greater_than_1: function () { return getdata > 1 }
})

and then in template

{{#if check_greater_than_1}}

You also may check for helpers with argument like

function check_greater_than(a,b) { return a > b }

usage

{{#if check_greater_than {{getdata}} 1}}

Checkout helpers in guide.


#3

Thank you so much sir. Actually problem is that I have this code.

{{#if getdata}}
//true

{{else}}

//false

{{/if}}

Now problem is that even if my helper method returns null data it returns true. I don't want to show anything when returned value is null.


#4

The {{#if condition checks the existing of the var so that is why its true

create yourself a helper

function check_getdata() { return getdata && getdata !== null }

#5

So I have to create 2 helpers ? :open_mouth:


#6

Maybe, depends on your tests

Otherwise try to install undescore helper like https://github.com/gwendall/meteor-underscore-helper

Then you may use ‘_’ and its function like ‘_.undefined’ in your templates


#7

Use this package: https://atmospherejs.com/raix/handlebar-helpers


#8

Are you sure it is working on latest meteor 1.3.x ? Because I have tried several functionalities but not working in template.

What user id do I have: {{$.Meteor.userId}}


#9

@tomfreudenberg : How can I call my function in next helper function ?

"first" ()
{
    return "Hi";
},
"second" ()
{
   return this.first();
} 

This is not working.


#10

You need to place your functions outside the helpers like

let my_funcs = {
  first,
  second
}

and then use in helper just

"first" ()
{
    return my_func.first();
},
"second" ()
{
   return my_func.first();
}

This is also a good organization approach.

You may find this usefull from @themeteorchef https://themeteorchef.com/blog/simple-code-organization-techniques-in-meteor/


#11

I didn’t use {{$. but do use {{#if $gt XXX XXX}} with latest Meteor.


#12

Thank you so much guys for help. @tomfreudenberg @daveeel :slight_smile:


#13

accountID == members[i].user_id ? currentBalanceDetails = members[i] : null;
if we use condition then also use this type of method