javascript - Basic 'if' statements do not work in handlebars JS - Stack Overflow

I am using express-handlebars and have the following minimal template:<!DOCTYPE html><html>

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?

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?

Share Improve this question asked Aug 20, 2018 at 15:15 mikemaccanamikemaccana 124k110 gold badges430 silver badges533 bronze badges 8
  • Try == instead. – Code-Apprentice Commented Aug 20, 2018 at 15:17
  • 1 Handlerbars have if only for checking if value is true or false it's not if like in javascript for this you will need to created your own helper. – jcubic Commented Aug 20, 2018 at 15:19
  • 3 Wow: stackoverflow./a/16315366/1225328. I'm curious to know the reasoning behind this "no boolean operator" choice they made to be honest. – sp00m Commented Aug 20, 2018 at 15:20
  • @sp00m care to post another answer with this link or edit my answer? Think it's very helpful in this context ;-) – Lyubomir Commented Aug 20, 2018 at 15:24
  • 1 @sp00m There's an if, it's already not logic-less. The point is to minimize the amount of putation in views, as it's almost always easier to test that logic outside of templates (and that logic has a tendency to accrete, making it even harder). – Dave Newton Commented Aug 20, 2018 at 15:57
 |  Show 3 more ments

2 Answers 2

Reset to default 6

Handlebar's if-helper only accepts a boolean as an argument. You have two options:

Use the existing handler

Pass the result from page === 'help' as a variable in the template and do something like:

{{#if isPageHelp}}
  <h1> Help! </h1>
{{/if}}

Make your own handler

You can implement the === operator with your own handler. Thanks @sp00m.

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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742283538a4414923.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信