Value converters (pipes)
Get to know Aurelia's value converters (pipes) and how to use them to transform data in your templates.
Value converters transform data as it flows between your view and view model. They’re ideal for formatting text, dates, currencies, and more. In other frameworks, you might know them as pipes.
Data Flow
Converters work in two directions:
toView: Prepares model data for display.
fromView: Adjusts view data before updating the model (useful with two-way binding).
Both methods receive the primary value as the first argument, with any extra arguments used as configuration.
Example Methods
Applying Converters in Templates
Use the pipe symbol (|
) to attach a converter:
A matching converter might be:
Chaining Converters
You can chain multiple converters by separating them with additional pipes:
Passing Parameters
Pass parameters using a colon (:
). Parameters can be:
Static:
Bound:
Object-based:
In both toView
and fromView
, the second parameter will be the passed configuration.
Receiving the Caller Context
By default, value converters receive only the value to transform and any configuration parameters. In some advanced scenarios, you may need to know more about the binding or calling context that invoked the converter—for example, to adjust the transformation based on the host element, attributes, or other binding-specific state.
Aurelia 2 provides an opt-in mechanism to receive the binding instance itself as an additional parameter. To enable this feature:
Add
withContext: true
to your value converter class:
Then use your converter in templates as usual:
At runtime, Aurelia will detect withContext: true
in the value converter and pass the binding instance as the second parameter. Depending on how the converter is used:
Property Binding (
foo.bind
orattr.bind
): the caller is aPropertyBinding
instanceInterpolation (
${ }
with converters): the caller is anInterpolationPartBinding
instanceOther Bindings: the caller corresponds to the specific binding type in use
Common Use Cases
Logging or debugging which binding invoked the converter
Applying different formatting based on binding context
Accessing binding metadata or context not available through standard converter parameters
Use this feature sparingly, only when you truly need insights into the calling context. For most formatting scenarios, simple converter parameters and camelCase converter names are sufficient.
Inspecting the Caller
Once withContext: true
is enabled and a converter receives the caller
parameter (an object with source
and binding
properties), you have several ways to learn more about the binding and component that invoked it:
Binding Instance (
caller.binding
): Access the binding instance directly. You can check its class to know which binding type triggered it.Component/View Model (
caller.source
): If the converter is used inside a component's binding,caller.source
provides the component instance (view-model).Relationship between
source
andbinding
:Property & Attribute Binding (e.g.,
value.bind="expr | myConverter"
on<input>
, orcustom-attr.bind="expr | myConverter"
):caller.binding
: ThePropertyBinding
instance.caller.source
: The view-model of the closest custom element containing this binding, if applicable.
Custom Element Interpolation (e.g.,
<my-element> ${ value | myConverter } </my-element>
):caller.binding
: TheInterpolationPartBinding
instance.caller.source
: The view-model ofmy-element
(or the closest custom element containing the interpolation).
By combining these properties, your converter can adapt its logic based on exactly which binding and component invoked it.
Creating Custom Value Converters
A converter is a simple class. Always reference it in camelCase within templates.
A no-op converter example:
Date Formatter Example
This converter formats dates based on locale:
Import it in your view:
Usage examples:
More Converter Examples
Currency Formatter
Formats numbers as currency strings.
Emoji Converter
Replaces keywords with emojis.
Leet Speak Converter
Transforms text into “1337” speak.
Upside Down Text Converter
Flips text upside down.
Ordinal Suffix Converter
Adds ordinal suffixes to numbers (1st, 2nd, 3rd, etc.).
Morse Code Converter
Transforms text into Morse code.
These examples highlight the flexibility of Aurelia 2's value converters. Experiment with your own transformations to tailor data exactly as you need it.
Last updated
Was this helpful?