Skip to content

Commit e828b25

Browse files
committed
add couple of remarks regarding inline/block usage of -if-
1 parent b8a1a4c commit e828b25

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
@@ -107,6 +107,24 @@ Like many programming languages, Ember also allows you to write `if else` and
107107
{{/if}}
108108
```
109109

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

111129
## Inline `if`
112130

@@ -218,6 +236,30 @@ It looks similar to a ternary operator.
218236
{{if condition value1 value2}}
219237
```
220238

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

222264
## Learn More
223265

0 commit comments

Comments
 (0)