Only show an element if it's NOT true in Handlebars?

Answered
coderguy asked this question 6 months ago
coderguy on Mar 24

How do I show an element in my handlebars (.hbs) only if it's not true.

I've tried to do {{#if not}}, {{#!if}}, {{#if !condition}}. But those don't work.

Any ideas?

I'm currently just doing:

{{#if condition}}
{{else}}
<button>Submit</button>
{{/if}}

And am looking to simplify it to not use the if...else statement.

2 suggested answers
nick on May 12

You're looking for the {{#unless}} helper. It serves as the inverse of the if helper you're attempting to use.

You could use it like this in your Handlebars code:

{{#unless condition}}
  <button>Submit</button>
{{/unless}}

This would show the <button> element when the condition you provide to the helper is falsey. Otherwise, it would render nothing.

0 replies
Answered