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

coderguy asked this question 10 months ago
coderguy on Mar 24, 2022

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

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

2 suggested answers
nick on May 12, 2022

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

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

0 replies