E
E
Eugene2019-08-02 14:27:30
JavaScript
Eugene, 2019-08-02 14:27:30

How to use custom helper in each?

How to use the custom helper is clear. But how to use and register it if I have an array and I iterate through it through each?
Small example (working jsfiddle.net )

var dataTest = {
  total: 4,
  invoices:  [
    {
      id: 1,
      var1: 354,
      var2: 'Test1',
    }, {
    	id: 2,
      var1: 300,
      var2: 'Test2',
    }, {
      id: 3,
      var1: 124,
      var2: 'Test3',
    }, {
      id: 4,
      var1: 354,
      var2: 'Test4',
    },
  ]
};
        
var template = Handlebars.compile($('#templateInvoice').html());
$('#invoiceList').html(template(dataTest));

How can I make a condition, for example, on the value of var1, in each invoices.
As I understand it, before I use the template, I need to first iterate over the array with my invoices, and register the helper?
data.invoices.forEach(function(item, i, arr) {
    Handlebars.registerHelper('var1check', function(item.var1) {
        if (item.var1 == 354) {
            return 'Значение 1';
        } else {
            return 'Значение 2';
        }   
    });
});

Or I misunderstood the scheme of using custom helpers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-02
@atachrus

https://jsfiddle.net/emw9zxnr/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question