Reputation: 123470
I am using express-handlebars
and have the following minimal template:
<!DOCTYPE html>
<html>
<body>
{{#if page === 'help'}}YAAAY{{/if}}
</body>
</html>
This fails to parse with:
Error: C:\Users\mike\myapp\views\error.hbs: Parse error on line 6:
...ody> {{#if page === 'help'}}YAAAY{{/i
---------------------^
I understand handlebars isn't expecting an ===
, but isn't that the point of if
?
How can I use an if
statement in handlebars?
Upvotes: 4
Views: 5090
Reputation: 53
Try this helpers
const Handlebars = require('handlebars');
Handlebars.registerHelper('ifCond', function (v1,v2,options) {
if (v1 == v2)
return options.fn(this);
else
return options.inverse(this);
});
Handlebars.registerHelper('exCond', function (v1,v2,options) {
if (v1 != v2)
return options.fn(this);
else
return options.inverse(this);
});
Upvotes: 1
Reputation: 20037
Handlebar's if-helper
only accepts a boolean as an argument. You have two options:
Pass the result from page === 'help'
as a variable in the template and do something like:
{{#if isPageHelp}}
<h1> Help! </h1>
{{/if}}
You can implement the ===
operator with your own handler. Thanks @sp00m.
Upvotes: 6