Conditional “if statement” helper for Handlebars.js

后端 未结 2 1059
深忆病人
深忆病人 2021-02-04 05:21

I am trying to write a conditional if statement helper for Handlebars.js. Essentially, I want to put an \"active\" class on a link if it is the Apply Now page.

相关标签:
2条回答
  • 2021-02-04 06:12

    NOTE: block(this) in the helper will not work anymore. Instead, use block.fn(this)

    e.g.

     Handlebars.registerHelper('isApplyNow', function(block) {
        if (this.title === "Apply Now") 
          return block.fn(this);
        else 
          return block.inverse(this);
     });
    
    0 讨论(0)
  • 2021-02-04 06:25

    I do see one minor syntax mistake which I believe could be the issue. If you are going to use a helper that takes a block, then you have to close it with the helper name. See how I've replaced your {{/if}} with {{/isApplyNow}}, like so:

        {{#isApplyNow}}
          <a href="{{url}}" class ='active'>{{this.title}}</a>
        {{else}}
          <a href="{{url}}">{{this.title}}</a>
        {{/isApplyNow}}
    
    0 讨论(0)
提交回复
热议问题