Skip to content

Commit e7a5922

Browse files
ciurlocks
authored andcommitted
add couple of remarks regarding inline/block usage of -if-
1 parent 28a66e1 commit e7a5922

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

guides/release/components/conditional-content.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ Like many programming languages, Ember also allows you to write `if else` and
114114
{{/if}}
115115
```
116116

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+
```
117135

118136
## Inline `if`
119137

@@ -225,6 +243,30 @@ It looks similar to a ternary operator.
225243
{{if condition value1 value2}}
226244
```
227245

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+
228270

229271
## Learn More
230272

0 commit comments

Comments
 (0)