Value converters (pipes)
Use value converters to transform how values are displayed in your applications. You can use value converters to transform strings, format dates, currency display and other forms of manipulation. They can be used within interpolation and bindings, working with data to and from the view.
If you have worked with other libraries and frameworks, you might know value converters by another name; pipes.
Understanding value converter data flow
Most commonly, you'll create value converters that translate model data to a format suitable for the view; however, there are situations where you'll need to convert data from the view to a format expected by the view model, typically when using two-way binding with input elements.
toView
The toView
method always receives the supplied value as the first argument, and subsequent parameters are configuration values (if applicable). This specifies what happens to values going to the view and allows you to modify them before display.
fromView
The fromView
method always receives the supplied value as the first argument, and subsequent parameters are configuration values (if applicable). This specifies what happens to values going out of the view to the view model and allows you to modify them before the view model receives the changed value.
Using value converters
To apply a value converter, you use the pipe |
character followed by the name of the value converter you want to use. If you have worked with Angular before, you would know value converters as pipes.
While Aurelia itself comes with no prebuilt value converters, this is what using them looks like for an imaginary value converter that converts a string to lowercase.
The code for this value converter might look something like this:
Transforming data using multiple value converters and parameters
Multiple value converters
Value converters can be chained, meaning you can transform a value and then transform it through another value converter. To chain value converters, you separate your value converters using the pipe |
. In this fictitious example, we are making our value lowercase and then running it through another value converter called bold which will wrap it in strong
tags to make it bold.
Parameter based value converters
You can also create value converters that accept one or more parameters. For value converters that format data, you might want to allow the developer to specify what that format is for, say, a currency or date formatting value converter.
Parameters are supplied using the colon :
character, and like the pipe, for multiple parameters, you can chain them. Parameters can be supplied as one or more strings (passed to the value converter method as one or more arguments) or a singular object.
Static parameters
Furthermore, value converter parameters also support bound values. Unlike other types of binding, you only have to supply the variable, which will bind it for you.
Bound parameters
Object parameters
If your value converter is going to have a lot of parameters, the existing approaches will fall apart quite quickly. You can specify your value converters take a single object of one or more parameters. Object parameters will also let you name them, unlike other parameters.
On our fromView
and toView
methods, the second argument will be the supplied object we can reference.
Creating value converters
Create custom value converters that allow you to format how your data is displayed and retrieved in your views.
Like everything else in Aurelia, a value converter is a class. A value converter that doesn't do anything might look like this. Nothing about this example is Aurelia-specific and is valid in Javascript.
Value converters are always referenced as camelCase when used in your templates.
To teach you how value converters can be created, we will create a simplistic value converter called date, allowing us to format dates.
In this example, we will use a decorator valueConverter
to decorate our class. While you can use the ValueConverter
naming convention as we did above, it's important to learn the different ways you can create value converters.
Import your value converter in your view
This example value below will display June 22, 2021
in your view. Because our default date format is US, it will display as month-date-year.
The locale parameter we specified in our value converter supports a locale parameter, allowing us to change how our dates are displayed. Say you're in the UK or Australia. The default format is date-month-year.
To see our value converter in action, here is what it looks like:
Additional Value Converter Examples
To further highlight how useful value converters are, we will provide examples of different value converters you can use in your Aurelia applications.
Currency Formatting Converter
Formats a number as a currency string.
Emoji Converter
Converts specific keywords or phrases to emojis.
Leet Speak Converter
Converts regular text into 'leet' or '1337' speak, a form of internet slang.
Upside Down Text Converter
Flips the text upside down.
Ordinal Suffix Value Converter
Appends an ordinal suffix to a number (e.g., 1st, 2nd, 3rd, etc.).
Morse Code Converter
Converts text to Morse code.
These fun and unique value converters showcase the versatility of Aurelia's templating engine and provide an enjoyable and engaging way for developers to learn about custom value converters.
Last updated