TemplateFilterRegistry class

Registry for template filters that transform or process values during template rendering.

A TemplateFilterRegistry maintains a map of filter names to their corresponding functions, allowing templates to apply transformations dynamically. Filters can operate on strings, numbers, lists, maps, or any object, returning a processed value for rendering.

Responsibilities

  • Provide built-in filters for common string, number, and list operations.
  • Support registration of custom filters at runtime.
  • Resolve filters by name for use during template rendering.

Design Notes

  • Filters are functions of type TemplateFilter.
  • Built-in filters cover common tasks like string capitalization, length calculation, reversing, formatting numbers, and HTML/URL escaping.
  • Custom filters can be added using registerFilter.

Usage Example

final registry = FilterRegistry();

final uppercase = registry.getFilter('uppercase');
print(uppercase?.call('hello')); // 'HELLO'

registry.registerFilter('double', (value) {
  if (value is num) return value * 2;
  return value;
});

final doubleFilter = registry.getFilter('double');
print(doubleFilter?.call(10)); // 20

Constructors

TemplateFilterRegistry([bool registerBuiltInFilters = true])
Creates a new TemplateFilterRegistry and registers all built-in filters.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getFilter(String name) TemplateFilter?
Retrieves a filter by its name.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerFilter(String name, TemplateFilter filter) → void
Registers a custom filter with the given name and filter function.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited