For the complete documentation index, see llms.txt. This page is also available as Markdown.

Formatting and Localization

Formatting happens at the display level in the Web Components' HTML templates. It is done by adding a formatter function to your regular data bindings.

These formatters only affect the visual output and leave the original data unchanged. Formatters are ordinary functions and may take multiple parameters or none at all.

There are several pre-defined formatters, but you also have the option to define your own.

Unformatted:

<ff-record>{{variantValues.0.Price}}</ff-record>

Formatted with $, the pre-defined price formatter function:

<ff-record>{{$ variantValues.0.Price}}</ff-record>

<!-- With a second parameter to override default formatting precision to zero decimals. -->
<ff-record>{{$ variantValues.0.Price 0}}</ff-record>

Formatters can be used in all data bindings.

Setup

The formatters use the web platform's native localization API. See documentation at MDN.

For them to work correctly, you have to configure your required locale.

During app initialization, you configure locale and formatting options in appConfig's formatting parameter. This is an object with two properties: locale and formatOptions.

These are the same values that you would pass to the platform-native Intl.NumberFormat(locales, options) constructor. See MDN for details.

Example:

Built-in formatters

Web Components offers several default formatters for currency and generic decimal numbers, but you may also specify your own in case the default formatters don't satisfy your requirements.

The following are the built-in formatters:

$ (number, fractionDigits)

Formats its first parameter according to the currency configuration.

The second parameter is optional and allows you to override your global settings for the number of decimal places.

$dec (number, fractionDigits)

Formats numbers according to your locale settings without any units.

The second parameter is optional.

$bctFilter

This currency formatter takes no arguments and only works in ff-breadcrumb-trail-item elements with type="filter".

It takes its input automatically from the HTML template context. This specialized formatter is necessary because of the special data structure provided in the ff-breadcrumb-trail-item template context.

If $bctFilter cannot detect how to format the received value, it will simply output the unformatted value. You can therefore use it safely even when the breadcrumb trail item displays a non-currency filter value.

$facetElement

This currency formatter takes no arguments and only works in ff-filter-cloud and ff-asn-group-element.

It formats eligible numeric values that it receives from the HTML template context. Ineligible values are output unchanged.

ff-hitHighlight (text)

This formatter is available in elements ff-suggest and ff-asn.

It takes one argument of type String. When a search query, available in contexts of ff-suggest and ff-asn, matches part of the text, the matched portion is wrapped in a <span class="ffw-query"> element for highlighting via CSS.

No wrapping occurs if there is no match. Always use it together with triple curly braces.

Suppose the passed text is Gardening and the context's search term is garden.

Template setup:

Rendered output:

Custom formatters

If the default formatters don't meet your requirements, you have the option to define your own formatters.

Use the factfinder.utils.formatters.add(name, fn) method to register them. Note that you cannot overwrite existing formatters.

The function that you pass as the second argument receives all values that are passed to it from the HTML template. The this keyword will be a reference of the data object available in the HTML template context.

Use the function keyword instead of an arrow function if you want to use this.

If you use the allCaps formatter inside ff-record like in the example above, then inside the allCaps function the this keyword would point to the Record object that you can also access inside the ff-record HTML template itself.

Reminder to explore

Remember that you can always place a debugger statement in your custom formatter functions and investigate the received parameters.

Alternative approach

As an alternative to custom formatters, you can also use the factfinder.response.transform___ API to directly manipulate data fields and display them without any further formatting.

Was this helpful?