Handlebars Helper: Compare values

PHOTO EMBED

Mon Jun 07 2021 08:20:41 GMT+0000 (Coordinated Universal Time)

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

// in app.js
hbs.registerHelper(`ifEquals`, function (a, b, opts) {
    if (a.toString() === b.toString()) {
        return opts.fn(this);
    }
    return opts.inverse(this);
});

// in hbs template
{{#ifEquals name 'Foo'}}
      true
{{else}}
      false
{{/ifEquals}}
content_copyCOPY

This helper takes 2 values passed into a template, converts them to type string, then compares them.