n1md7
n1md7

Reputation: 3479

How to write one line If statement using Handlebars?

I would like to know what is the correct syntax of this:

<li class="nav-item {{# if undefined !== user}} hidden {{/if}}">

I want to add class name hidden if user variable exists.

It displays an error:

enter image description here

Upvotes: 1

Views: 1508

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

You can't use undefined !== user expression, it's not valid syntax. The if block helper will check for undefined, so there is no need for undefined !== user, just use: {{#if user}}

<li class="nav-item {{#if user}}non-empty{{/if}}">

If you want to check if user is empty you should use the unless helper

<li class="nav-item {{#unless user}}empty-user{{/unless}}">

Here's is the code for the default if helper, basically it will evaluate to true if non falsy value is passed.

Upvotes: 2

Related Questions