I am using the Express handlebars and I am stuck at the point to compare the values in IF ELSE block.Is there built in helper? I spent a lot of time to find the solution but I didn't get that. Help would be appreciated
I have fixed this. Reference:
Solution: To make equal '==' condition between two values using Express Helperbars do following steps.
1- Create helpers
hbs.js
module.exports = {
if_eq: function(a, b, opts) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
}
}
2- Register these Helpers in app.js file
// Handlebars Helpers
const {
if_eq
} = require('./helpers/hbs'); //path to hbs.js file
// Handlebars Middleware
app.engine('handlebars', exphbs({
helpers: {
if_eq:if_eq
},
defaultLayout: 'main'
}));
3- Use if_eq helper in your HTML code.I am using to check radio button if gender is 'male' and similar to 'female' case.
{{#if_eq idea.gender 'male'}}
Female<input type="radio" class="form-control" name="gender" value="female">
Male <input type="radio" class="form-control" name="gender" value="male" checked>
{{else}}
Female<input type="radio" class="form-control" name="gender" value="female" checked>
Male <input type="radio" class="form-control" name="gender" value="male" >
{{/if_eq}}