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.

Template expressions

Expressions inside ${} can perform operations such as arithmetic, function calls, or ternaries:

Addition example
<p>Quick maths: ${2 + 2}</p>
<!-- Outputs "Quick maths: 4" -->

Calling functions

You can call functions defined on your view model. For example:

my-app.ts
export class MyApp {
  adder(val1: number, val2: number): number {
    return parseInt(val1) + parseInt(val2);
  }
}
my-app.html
<p>Behold mathematics, 6 + 1 = ${adder(6, 1)}</p>
<!-- Outputs "Behold mathematics, 6 + 1 = 7" -->

Using ternaries

You can also use ternary operations:

This will display either "True" or "False" depending on the boolean value of isTrue.

Complex expressions

You can use more sophisticated expressions for dynamic content:

Optional Syntax

Aurelia supports the following optional chaining and nullish coalescing operators in templates:

  • ??

  • ?.

  • ?.()

  • ?.[]

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.

HTMLElement Interpolation

Aurelia supports passing HTMLElement objects directly to template interpolations. This allows you to dynamically create and insert DOM elements into your templates at runtime.

Creating elements with document.createElement()

You can create DOM elements in your view model and bind them directly:

The button element will be directly inserted into the div, maintaining all its properties and event listeners.

Parsing HTML strings

You can also parse HTML strings and render the resulting elements:

Security Considerations

When interpolating HTMLElements, be mindful of security implications:

Dynamic element creation

This feature is particularly useful for dynamic content scenarios:

Notes on syntax

While template interpolation is powerful, there are a few limitations to keep in mind:

  1. You cannot chain expressions using ; or ,.

  2. You cannot use certain primitives or operators such as Boolean, String, instanceof, or typeof.

  3. 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.

Performance Best Practices

Avoid Complex Expressions

Keep interpolation expressions simple for better performance. Complex computations should be moved to getters or methods:

Array Observation Performance

Aurelia automatically observes arrays used in interpolation. For large arrays that change frequently, consider using computed getters:

Memory Considerations

When using HTMLElement interpolation, ensure proper cleanup to avoid memory leaks:

Error Handling and Edge Cases

Handling Null and Undefined Values

Interpolation gracefully handles null and undefined values by rendering empty strings:

Error-Prone Expressions

Some expressions can throw runtime errors. Use defensive patterns:

Type Coercion Behavior

Interpolation converts values to strings following JavaScript coercion rules:

HTMLElement Edge Cases

When interpolating HTMLElements, be aware of these behaviors:

Advanced Example: Dynamic Content with Observer Updates

Here's an example showing how interpolation works with Aurelia's observer system to automatically update the view when data changes:

The interpolation is automatically updated by Aurelia's array observer whenever items are added to the collection.

Last updated

Was this helpful?