Text interpolation
Last updated
Was this helpful?
Last updated
Was this helpful?
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 .
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.
Expressions inside ${}
can perform operations such as arithmetic, function calls, or ternaries:
You can call functions defined on your view model. For example:
You can also use ternary operations:
This will display either "True" or "False" depending on the boolean value of isTrue
.
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.
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
, or typeof
.
The pipe character |
is reserved for Aurelia value converters and cannot be used as a bitwise operator inside interpolation.