How do I convert Handlebars isEq helper to Spacebars in Meteor?
Asked Answered
K

3

2

I've had in my Meteor project handlebar helper:

Handlebars.registerHelper('isEq', function(v1, v2, options){
    if(v1 === v2){
        return options.fn(this);
    }else{
        return options.inverse(this);
    }
});

But after update to 0.8 and switch from handlebars to spacebars it is not working anymore - I've found in other stackoverflow topic that now I should change Handlebars.registerHelper to UI.registerHelper but it is still not working - anyone know how to implement this properly for spacebars?

Koslo answered 26/4, 2014 at 9:21 Comment(3)
what happens, does it run this helper?Cosmorama
I'm getting Deps error in the console - something like this: Exception from Deps recompute function: Spacebars.call(...) and many, many lines of weird errors that says nothing to me.Koslo
possible duplicate of Check for equality in Spacebars?Was
L
3

You want to use it like the following?

{{#isEq 7 8}}
    They're equal!
{{else}}
    They're not equal :(
{{/isEq}}

From 0.8, block helpers are defined as templates. See https://github.com/meteor/meteor/tree/devel/packages/spacebars#custom-block-helpers

And I think you need to call it with keyword arguments ({{#isEq v1=7 v2=8}}). Although, you should be able to define isEq as an helper, and then use the #if block helper like {{#if isEq 7 8}}.

Liturgist answered 26/4, 2014 at 11:29 Comment(1)
I've changed my function to return true or false value and use it like this: {{#if isEq 2 3}} and it worked! Thank You.Koslo
A
6

I use a UI.registerHelper to add an eq function that can be used in conjunction with {{#if}} in Spacebars.

In the JavaScript code, I register an eq function of two variables that I can call from Spacebars (the system that has replaced Handlebars in Meteor v0.8)

UI.registerHelper('eq', function(v1, v2, options) {
  if(v1 == v2){
    return true
  } else {
    return false
  }
});

In the HTML, I write:

{{#if eq 1 2}}
They are equal.
{{else}}
They're not equal
{{/if}}
Aliquot answered 23/8, 2014 at 21:48 Comment(0)
L
3

You want to use it like the following?

{{#isEq 7 8}}
    They're equal!
{{else}}
    They're not equal :(
{{/isEq}}

From 0.8, block helpers are defined as templates. See https://github.com/meteor/meteor/tree/devel/packages/spacebars#custom-block-helpers

And I think you need to call it with keyword arguments ({{#isEq v1=7 v2=8}}). Although, you should be able to define isEq as an helper, and then use the #if block helper like {{#if isEq 7 8}}.

Liturgist answered 26/4, 2014 at 11:29 Comment(1)
I've changed my function to return true or false value and use it like this: {{#if isEq 2 3}} and it worked! Thank You.Koslo
P
2

This is how I implemented in Meteor 1.3+

// JavaScript
Template.registerHelper('odd', function(conditional, options) {
  return conditional % 2;
});

<!-- HTMLS -->
{{#each myCollection}}
{{#if (odd @index)}}
  even
{{else}}
  odd
{{/if}}
{{/each}}
Padauk answered 26/8, 2016 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.