Built-in Filters
abs
Theabs filter returns the absolute value of a number.
Syntax
number_value(number): The input number.
capitalize
Thecapitalize filter capitalizes the first character of a string.
Syntax
string_value(string): The input string to be capitalized.
dump
Thedump filter serializes a JavaScript object into a JSON string.
Syntax
object_value(any): The input object to be serialized.
join
Thejoin filter joins elements of an array into a single string, separated by a specified separator.
Syntax
array_value(any[]): The input array.separator(string, optional): The string to separate the array elements. Defaults to", ".
lower
Thelower filter converts a string to lowercase letters.
Syntax
string_value(string): The input string to be converted to lowercase.
replace
Thereplace filter replaces all occurrences of a specified substring with a new substring.
Syntax
string_value(string): The input string.search(string): The substring to search for.replace(string): The substring to replace with.
round
Theround filter rounds a number to a specified number of decimal places.
Syntax
number_value(number): The input number to be rounded.decimals(number, optional): The number of decimal places to round to. Defaults to0.
truncate
Thetruncate filter truncates a string to a specified length and appends an ellipsis (...) if necessary.
Syntax
string_value(string): The input string to be truncated.length(number): The maximum length of the output string.
upper
Theupper filter converts a string to uppercase letters.
Syntax
string_value(string): The input string to be converted to uppercase.
urlencode
Theurlencode filter encodes a string to be safe for use in URLs.
Syntax
string_value(string): The input string to be URL-encoded.
Creating Custom Filters
You can create custom filters by registering them with theFilterRegistry.
FilterRegistry.register
Register a custom filter function using the staticregister method:
name(string): The name used to call the filter in templates.filterFunction(FilterFunction): The function that performs the transformation.
FilterFunction Type
TheFilterFunction type signature is:
input- The first argument is always the value being filtered....args- Additional arguments passed to the filter.
Example: Custom Filter
Here’s an example of creating a customreverse filter that reverses a string:
Example: Filter with Arguments
Filters can accept additional arguments. Here’s apad filter that pads a string to a specified length: