@@ -107,6 +107,24 @@ Like many programming languages, Ember also allows you to write `if else` and
107
107
{{/if}}
108
108
```
109
109
110
+ An important feature of ` if ` statement is that it can be used only inside an
111
+ HTML element or another block. For example following snippet won't work
112
+ because it uses `` if `` statement ** on** the HTML element:
113
+
114
+ ``` handlebars {data-filename="app/components/sign-in.hbs"}
115
+ {{!-- Won't work --}}
116
+ <button {{#if @disabled}} disabled {{/if}} class="btn">Sign In</button>
117
+ ```
118
+
119
+ Correct usage:
120
+
121
+ ``` handlebars {data-filename="app/components/sign-in.hbs"}
122
+ {{#if @disabled}}
123
+ <button disabled class="btn">Sign In</button>
124
+ {{else}}
125
+ <button class="btn">Sign In</button>
126
+ {{/if}}
127
+ ```
110
128
111
129
## Inline ` if `
112
130
@@ -218,6 +236,30 @@ It looks similar to a ternary operator.
218
236
{{if condition value1 value2}}
219
237
```
220
238
239
+ Similarly to block ` if ` , you need to pay attention where inline ` if ` is placed
240
+ otherwise ember will be confused. Inline ` if ` can be used only inside attributes values
241
+ of HTML element. For example:
242
+
243
+ ``` handlebars {data-filename="app/components/spinner.hbs"}
244
+ <span class="spinner {{if @inProgress 'visible' 'invisible'}}">
245
+ </span>
246
+ ```
247
+
248
+ In example above, inline `` if `` is correctly used inside class attribute value.
249
+
250
+ On the other hand, if you intend to use inline `` if `` to add "disabled"
251
+ attribute to the HTML element:
252
+
253
+ ``` handlebars {data-filename="app/components/spinner.hbs"}
254
+ {{!-- Won't work (!) --}}
255
+ <span {{if @disabled 'disabled' }}">
256
+ </span>
257
+ ```
258
+
259
+ Ember will get confused and will complain with an error. In example above
260
+ Ember will see ` if ` as modifier instead and complain about incorrect usage of
261
+ modifier.
262
+
221
263
222
264
## Learn More
223
265
0 commit comments