Adding offset to {{@index}} when looping through items in Handlebars

前端 未结 9 1025
逝去的感伤
逝去的感伤 2020-12-24 01:20

I\'m iterating over a list in Handlebars using the built-in each helper. Within the each block, I\'m referencing the current loop index

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 02:00

    The handlebars-helpers library has a fairly thorough mathematics library in lib/math.js, including a general purpose {{add a b}} helper defined as follows:

    /**
     * Return the product of a plus b.
     *
     * @param {Number} a
     * @param {Number} b
     * @api public
     */
    helpers.add = function(a, b) {
      return a + b;
    };
    

    If you don't want to copy and paste this into your project and you have the possibility to use npm, you can get this dependency as follows:

    npm install handlebars-helpers --save

    Then, you can register the math helpers as follows:

    const handlebars = require('handlebars'),
      handlebarsHelpers = require('handlebars-helpers');
    
    handlebarsHelpers.math({
      handlebars: handlebars
    });
    

提交回复
热议问题