- Ember.js Cookbook
- Erik Hanchett
- 821字
- 2021-07-16 12:58:03
Working with conditionals in templates
Using conditionals is fundamental to using Ember's templating engine. In the following recipes, we'll take a look at conditionals and how they work with templates.
How to do it...
Let's take a look at a simple example that displays text if some property is true.
- Create a new project and generate a new controller called
conditional
. Run this command in the root of theapplication
folder to createcontroller
andtemplate
:$ ember g controller conditional $ ember g template conditional
This will create the conditional controller.
- Update the
router.js
file with the newconditional
route:// app/router.js … Router.map(function() { this.route('conditional'); });
This will add a new
conditional
route. To access this route using the Ember server, open a web browser and navigate tohttp://localhost:4200/conditional
. - Update the
conditional
controller with theisHomeworkDone
property:// app/controllers/conditional.js import Ember from 'ember'; export default Ember.Controller.extend({ isHomeworkDone: true});
This will create a new
isHomeworkDone
property and default it totrue
. - Update the conditional template so that it will display one message if
isHomeworkDone
istrue
and another message if it isn't:// app/templates/conditional.hbs Hello! {{#if isHomeworkDone}} Thanks for finishing the homework! {{else}} Please finish the homework {{/if}}
Note
The
{{if}}
statement is a helper and must be surrounded by curly braces{{}}
like any other Handlebar expression. It begins with a#
sign, which indicates that it's a form of a block invocation. The{{/if}}
statement closes the statement.The preceding example shows two statements,
{{if}}
and{{else}}
, both in the block form. Only the statement that is true will be displayed. - As we know from the controller earlier, if
isHomeworkDone
istrue
, the statementThanks for finishing the homework!
will be displayed after the template is rendered. On the other hand, ifisHomeworkDone
wasfalse
, the statementPlease finish the homework
will be displayed. - To test this example, navigate to the
http://localhost:4200/conditional
route. The{{outlet}}
inapplication.hbs
will render theconditional
template inside of it.
Using inline invocation with templates
Inline invocation can be used to display data with if
statements, all within one line of code.
- We'll take the previous example and recreate it using inline invocation. Edit the
condtional.hbs
file in theapp/templates
folder with the newif
statement using inline invocation:// app/templates/conditional.hbs Hello {{if isHomeworkDone 'Thanks for finishing the homework!' 'Please finish the homework'}}
- When using inline invocation, you don't need to use the pound sign
#
or end theif
block with{{/if}}
. Everything can be written in one expression. - The first argument of the helper after
isHomeworkDone
,Thanks for finishing
the homework!
, will be shown only ifisHomeworkDone
istrue
. The second argument,Please finish the homework
, will be displayed ifisHomeworkDone
isfalse
.
Working on nested invocation with templates
Nested invocations are inline, which means that they return a single value. They can also accept multiple nested if
statements in the inline helper.
- In the
conditional
controller, add a couple of properties calledisHomeworkDone
andisChoresDone
:// app/controllers/conditional.js import Ember from 'ember'; export default Ember.Controller.extend({ isHomeworkDone: true, isChoresDone: true});
Both of these are defaulted to
true
. - Let's use nested invocation to display a message only if both
isHomeworkDone
andisChoresDone
aretrue
. Edit thecondtional.hbs
file with the new nestedif
statement:// app/templates/conditional.hbs Hello {{if isHomeworkDone (if isChoresDone 'Thanks for finishing the homework!' )}}
The
Thanks for finishing the homework
string will display only if bothisChoresDone
andisHomeworkDone
aretrue
. Otherwise, nothing is displayed. As the controller has both values set totrue
, the message will display Thanks for finishing the homework! after the template is rendered.
The opposite of if is unless
Another useful helper is unless
. It works exactly the opposite of the if
helper. It can work with all three invocation styles—inline, block, and nested.
We'll create the unless
block that will display a string if it's not true in our conditional.hbs
file:
// app/templates/conditional.hbs Hello {{#unless isHomeworkDone}} Please finish the homework {{else}} Thanks for finishing the homework! {{/unless}}
In this block, the unless
helper will display Please finish the homework
only if isHomeworkDone
is false
. On the other hand, the message Thanks for finishing the homework!
will be displayed if isHomeworkDone
is true
. This is essentially the opposite of the if
helper.
In this example, assuming that isHomeworkDone
is true
, the Thanks for finishing the homework!
string will be displayed in the template after it's rendered.
How it works...
The if
and unless
conditionals are built-in helpers that are made available to us from the Handlebars templating engine. They are surrounded by curly braces {{}}
, which tell Handlebars to interpret them. The {{if}}
statement checks whether the property is true
. JavaScript values such as undefined
, null
, ''
, []
, and numeric 0
will return as false
.
There are three different ways in which these conditional helpers can be invoked—block, nested, or inline. All three will work with if
and unless
helpers.
- 精通Nginx(第2版)
- Python概率統計
- JavaScript前端開發模塊化教程
- JavaScript 從入門到項目實踐(超值版)
- .NET 4.0面向對象編程漫談:基礎篇
- 看透JavaScript:原理、方法與實踐
- Java持續交付
- 概率成形編碼調制技術理論及應用
- Python深度學習:基于TensorFlow
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- Mastering Backbone.js
- Kubernetes源碼剖析
- OpenStack Networking Essentials
- 每個人的Python:數學、算法和游戲編程訓練營
- Perl 6 Deep Dive