Handlebars Helper: Compare values with ===, >, <

PHOTO EMBED

Tue Jun 08 2021 16:48:15 GMT+0000 (Coordinated Universal Time)

Saved by @hisam #hbs #handlebars #express #nodejs

hbs.registerHelper(`iff`, function (a, operator, b, opts) {
    let bool = false;
    a.toString();
    b.toString();
    switch (operator) {
        case `===`:
            bool = a === b;
            break;
        case `>`:
            bool = a > b;
            break;
        case `<`:
            bool = a < b;
            break;
        default:
            bool = a === b;
    }

    if (bool) {
        return opts.fn(this);
    }
    return opts.inverse(this);
});

{{#iff value1 '>' value2}}
do the thing
{{/iff}}
content_copyCOPY

Compare less than, equal to, greater than