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