Use an if else statement in a Handlebars (.hbs) file?

Answered
coderguy asked this question 1 year, 6 months ago
coderguy on Mar 24, 2022 · Edited

In my Handlebars templates, I need to add if...else conditionals to dictate what renders on certain pages.

I need it do something like this this:

<td>
  if 
    <button>Is Owner</button>
  else
    <button>Not Owner</button>
</td>

Thanks for the help in advance!

2 suggested answers
nick on May 14, 2022

In Handlebars, you can do something like this:

<td>
  {{#if condition}}
    <button>Is Owner</button>
  {{else}}
    <button>Not Owner</button>
  {{/if}}
</td>

Based on the condition value you pass to the template, one or the other version of the button will be used.

0 replies
itsbambi on May 14, 2022 · Edited

Handlebars has a built in {{#if}} helper you can use.

More info here: https://handlebarsjs.com/guide/builtin-helpers.html#if.

They have a good example of an if...else here: https://handlebarsjs.com/examples/builtin-helper-ifelse-block.html.

0 replies
Answered