Search code examples
meteormeteor-blazespacebars

Meteor Invert Boolean Helper


This may seem like a simple question, but it is one that I have struggled to find documentation for.

I have a spacebars helper that returns values from a collection, in a cursor of objects for use in an {{#each}} block. These objects have a boolean property that I use to check/uncheck a checkbox.

However, the boolean values in the database need to be inverted for use in the checkbox. If a record in the collection has the boolean property evaluating to "false," I need it to be "true" in usage.

{{#each records}}
{{name}}: <input type="checkbox" checked="{{!checked}}">
{{/each}}

The issue here is that {{! signals a spacebars comment, rather than converting "false" to "true."
In this snippet, {{!checked}} is considered a comment rather than a helper.

Theoretically, I could run a forEach() loop in the helper logic and invert the boolean values for each object. However, I feel like there must be a better way for something as simple as this.


Solution

  • Just make yourself a not global helper:

    Template.registerHelper('not',(param)=>{
      return !param;
    });
    

    Then in any template use {{not checked}}