Text interpolation
Text interpolation allows you to display dynamic values in your views. By wrapping an expression with ${}
, you can render variables, object properties, function results, and more within your HTML. This is conceptually similar to JavaScript template literals.
Displaying values with interpolation
Interpolation can display the values of view model properties, object fields, and any valid expression. As an example, consider the following code:
Here, the template references the same property name, myName
, that is defined in the view model. Aurelia automatically replaces ${myName}
with "Aurelia" at runtime. Any property you define on your class can be directly accessed inside your templates.
Template expressions
Expressions inside ${}
can perform operations such as arithmetic, function calls, or ternaries:
Calling functions
You can call functions defined on your view model. For example:
Using ternaries
You can also use ternary operations:
This will display either "True" or "False" depending on the boolean value of isTrue
.
Optional Syntax
Aurelia supports the following optional chaining and nullish coalescing operators in templates:
??
?.
?.()
?.[]
Note that ??=
is not supported.
You can use these operators to safely handle null or undefined values:
This helps avoid lengthy if-statements or ternary checks in your view model when dealing with potentially undefined data.
Notes on syntax
While template interpolation is powerful, there are a few limitations to keep in mind:
You cannot chain expressions using
;
or,
.You cannot use certain primitives or operators such as
Boolean
,String
,instanceof
, ortypeof
.The pipe character
|
is reserved for Aurelia value converters and cannot be used as a bitwise operator inside interpolation.
For complex transformations or formatting, consider using Aurelia’s value converters instead of cramming too much logic into an interpolation.
Last updated
Was this helpful?