Odd and even number comparison helper for Handlebars

11,605

I created this helper and it worked

Handlebars.registerHelper('if_even', function(conditional, options) {
  if((conditional % 2) == 0) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

Just followed conditional helper here http://handlebarsjs.com/block_helpers.html

I tried to do this based on mu is too short suggestion:

{{#if_even @index}}
Share:
11,605
HP.
Author by

HP.

Updated on July 11, 2022

Comments

  • HP.
    HP. almost 2 years

    I am trying to find a way to parse out differently depending array index as odd or even number

    I was looking at this http://assemble.io/helpers/helpers-comparison.html and hope to find something like this:

    {{#each array}}
    {{#if_odd {{@index}}}}
        {{this}} is odd 
    {{else}}
        {{this}} is even
    {{/if_odd}}
    {{/each}}
    

    I don't really care about the syntax but hope my idea comes across. Any help? Thanks.