218

I have a statement like this:

{{#if IsValid}}

I want to know how I can use a negative if statement that would look like that:

{{#if not IsValid}}
bartocc
  • 727
  • 1
  • 7
  • 19
Kapil Garg
  • 3,100
  • 5
  • 19
  • 19

6 Answers6

499

Simple answers for simple questions:

{{#unless isValid}}
{{/unless}}

Also keep in mind that you can insert an {{else}} in between an {{#if}} or {{#unless}} and the closing tag.

Christopher Swasey
  • 10,392
  • 1
  • 31
  • 25
53

You have many ways of doing that.

1. Use {{unless}}:

{{#unless isValid}}
  ...
{{else}}
  ...
{{/unless}}

2. Use inline-if helper:

{{#if (if isValid false true)}}
  ...
{{else}}
  ...
{{/if}}

3. Use ember-truth-helpers addon:

{{#if (not isValid)}}
  ...
{{else}}
  ...
{{/if}}
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • 1
    Found quite useful to use in next way: {{input type="text" ... disabled=(not someProperty) ...}} – lesyk Sep 29 '16 at 06:37
9

it can be done in multiple ways.

1 use unless

{{#unless IsValid}}
<Your Code>
{{/unless}}

2.use if else

{{#if IsValid}}
{{else}}
<Your Code>
{{/if}}

3.use not helper

{{#if (not IsValid)}}
<Your Code>
{{/if}}
Nitin9791
  • 1,124
  • 1
  • 14
  • 17
3

unless block helper (built-in helper)

unless helper is the inverse of the if helper.

Its block will be rendered if the expression returns a falsy value.

  {{#unless valid}}
  <h3 class="warning">WARNING</h3>
  {{/unless}}
XY L
  • 25,431
  • 14
  • 84
  • 143
1
{{#if items.length}}
    //Render
{{/if}}

Here items.length .. if it returns some value except null, then only it will enters into the if loop.

NOTE : You can check Boolean values also. In If block

{{#if booleanFloag}}
jjm
  • 431
  • 3
  • 19
1

Below Statements Will help full if you want to use if and else :

{{#if author}}
    <h1>{{firstName}} {{lastName}}</h1>
{{else}}
    <h1>Unknown Author</h1>
{{/if}}

NOTE : Dont close the if Block until logic finished ...