Skip to main content
Filters are functions that take an input value and return a transformed output. TemplateDX ships ten built-in filters and exposes two ways to register your own: a global static API and a per-instance API on TemplateDX.

Creating custom filters (TypeScript)

FilterRegistry.register (static or instance API)

Static (global) registration is the simplest path; everything using the default transform/stringify exports sees it:
Parameters
  • name (string): The name used to call the filter in templates.
  • filterFunction (FilterFunction): The function that performs the transformation.
Instance (scoped) registration is for when you want filters isolated per engine:
new TemplateDX({ includeBuiltins: true }) copies the built-in filters into the instance; pass false to start empty.

FilterFunction type

The FilterFunction type signature is:
  • input - The first argument is always the value the filter receives.
  • ...args - Additional arguments passed to the filter.

Example: custom filter

Here’s an example of creating a custom reverse filter that reverses a string:
Usage:
Output

Example: filter with arguments

Filters can accept additional arguments. Here’s a pad filter that pads a string to a specified length:
Usage:
Output

Creating custom filters (Python)

agentmark-templatedx (Python) mirrors the TS surface. Define a function and register it via the static (register_global) or instance API:
Unlike the tag registry (which takes the plugin first), filter registration takes the name first in both languages: register_global(name, func). For instance-scoped registration, construct the engine and use register_filter:

Built-in filters

abs

The abs filter returns the absolute value of a number. Syntax
Parameters
  • number_value (number): The input number.
Example
Output

capitalize

The capitalize filter capitalizes the first character of a string. Syntax
Parameters
  • string_value (string): The input string to capitalize.
Example
Output

dump

The dump filter serializes a JavaScript object into a JSON string. Syntax
Parameters
  • object_value (any): The input object to serialize.
Example
Output
The leading backslash comes from the final stringify step, which escapes { at the start of text output. This applies to any filter output that begins with { or [.

join

The join filter joins elements of an array into a single string, separated by a specified separator. Syntax
Parameters
  • array_value (any[]): The input array.
  • separator (string, optional): The string to separate the array elements. Defaults to ", ".
Example
Output

lower

The lower filter converts a string to lowercase letters. Syntax
Parameters
  • string_value (string): The input string to convert to lowercase.
Example
Output

replace

The replace filter replaces all occurrences of a specified substring with a new substring. Syntax
Parameters
  • string_value (string): The input string.
  • search (string): The substring to search for.
  • replace (string): The substring to replace with.
Example
Output

round

The round filter rounds a number to a specified number of decimal places. Syntax
Parameters
  • number_value (number): The input number to round.
  • decimals (number, optional): The number of decimal places to round to. Defaults to 0.
Example
Output

truncate

The truncate filter truncates a string to a specified length and appends an ellipsis (...) if necessary. Syntax
Parameters
  • string_value (string): The input string to truncate.
  • length (number): The maximum length of the output string.
Example
Output
truncate takes the first length characters and appends ..., so the output is length + 3 characters total.

upper

The upper filter converts a string to uppercase letters. Syntax
Parameters
  • string_value (string): The input string to convert to uppercase.
Example
Output

urlencode

The urlencode filter encodes a string to be safe for use in URLs. Syntax
Parameters
  • string_value (string): The input string to be URL-encoded.
Example
Output
urlencode uses encodeURIComponent, which leaves ! * ' ( ) unencoded by spec.

Next steps

Filters transform values inside expressions; for control flow and custom block behavior, see Tags.