简体   繁体   中英

How Should I Properly Return True/False From a Meteor Template Helper?

its there a simple way to accomplish this.

Template.example.helpers({
 showElement : function(){
 var number = Session.get('number')
 if(number === 1){
     console.log(number)
    return true;
   } else {
    console.log(number)
    return false;
   }
  }
})

For example lets say i have this 2 events setting to 1 and to 0

//Setting to 1
Template.example.events({
'click #setToOne' : function(e,t){
   Session.set('number',1)
  }
})
 //Setting to 0
Template.example.events({
'click #setToZero' : function(e,t){
   Session.set('number',0)
  }
})

So this is actually working because the console.log inside the helper are showing 0 and 1

But how to call that helper on the template ?

Already try this

<template name="example">
{{#if showElement.true}}
 <h1>show this header if Truee</h1>
{{else}}
<h1>show this header if False</h1>
{{/if}}
</template>

So what this is a best practice? or should i use UI.registerHelper? (i don't really know how to works with UI.registerHelpers)

Thanks for the help in advice

<template name="example">
{{#if showElement}}
 <h1>show this header if Truee</h1>
{{else}}
<h1>show this header if False</h1>
{{/if}}
</template>

Let's take your problem and make a generic session variable equality checker helper:

Template.registerHelper('checkSession',function(sessionVarName,compareTo){
  return ( Session.get(sessionVarName) === compareTo );
});

With your specific variable name and comparison you could use this from any template with:

{{#if checkSession 'number' 1}}

which translates to if the session variable labeled 'number' is === 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM