Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd and even number comparison helper for Handlebars [closed]

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.

like image 775
HP. Avatar asked Sep 24 '13 08:09

HP.


People also ask

How do you compare two values in Handlebars?

{{if}} is used to check whether a field has a value or not, whereas {{compare}} is used to check whether a field has one value or another value. Check out our Handlebars helper reference for more information on handlebars {{compare}}, {{if}}, and other handlebars expressions!

What is helper in Handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

What are Handlebars options?

The options -parameter In addition to the parameters used in the helper-call, an options -object is passed to the helper as additional parameter. lookupProperty(object, propertyName) : a function that returns an "own property" of an object.

How do I add Handlebars to my helper?

You can register expression Helper by using the following code: Handlebars. registerHelper("last", function(array) { return array[array. length - 1]; });


1 Answers

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}}
like image 172
HP. Avatar answered Oct 16 '22 07:10

HP.