Template filters

Use the filter classes and default mapping with Template. See Templating for the language and built-in filter table, and Writing template filters for the custom filter tutorial, lifecycle, and remote transfer examples.

Pickleable template filter classes and the default name-to-class mapping.

class rmote.templates.filters.CapitalizeFilter[source]

Bases: TemplateFilter

Capitalize a string.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.DefaultFilter[source]

Bases: TemplateFilter

Replace missing values, and also false values when boolean=True.

__call__(value, default_value='', boolean=False)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

handles_undefined = True
class rmote.templates.filters.FirstFilter[source]

Bases: TemplateFilter

Return the first item, or an undefined result for an empty iterable.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.FloatFilter[source]

Bases: TemplateFilter

Convert to float, returning the fallback on a failed conversion.

__call__(value, default=0.0)[source]

Transform the piped value with the supplied filter arguments.

Return type:

getdouble

class rmote.templates.filters.IPAddressFilter[source]

Bases: TemplateFilter

Parse an interface, optionally returning an IP operation as plain data.

Without action, return the ipaddress.ip_interface() object. action is interface, address, network, network_address, netmask, hostmask, broadcast, prefix, or version. Prefix and version are integers; other results are strings. Parsing uses ipaddress.ip_interface() to retain the supplied prefix.

>>> IPAddressFilter()("192.0.2.7/24")
IPv4Interface('192.0.2.7/24')
>>> IPAddressFilter()("192.0.2.7/24", "network")
'192.0.2.0/24'
>>> IPAddressFilter()("2001:db8::7/64", action="prefix")
64
__call__(value, action=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

IPv4Interface | IPv6Interface | Text | getint

class rmote.templates.filters.IntFilter[source]

Bases: TemplateFilter

Convert to int, accepting decimal float strings and a fallback value.

__call__(value, default=0, base=10)[source]

Transform the piped value with the supplied filter arguments.

Return type:

getint

class rmote.templates.filters.JoinFilter[source]

Bases: TemplateFilter

Join stringified items, optionally selecting an attribute first.

__call__(value, d='', attribute=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Text

class rmote.templates.filters.LastFilter[source]

Bases: TemplateFilter

Return the last item of a reversible sequence, or an undefined result.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.LengthFilter[source]

Bases: TemplateFilter

Return the length of a sized value.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.ListFilter[source]

Bases: TemplateFilter

Materialize an iterable as a list.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.LowerFilter[source]

Bases: TemplateFilter

Convert to a lowercase string.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.ReplaceFilter[source]

Bases: TemplateFilter

Replace occurrences after converting the value and replacements to strings.

__call__(value, old, new, count=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Text

class rmote.templates.filters.ReverseFilter[source]

Bases: TemplateFilter

Reverse a string or return an iterator over an iterable in reverse order.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.SortFilter[source]

Bases: TemplateFilter

Stable sort; strings compare case-insensitively unless requested otherwise.

__call__(value, reverse=False, case_sensitive=False, attribute=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

list[Any]

class rmote.templates.filters.StringFilter[source]

Bases: TemplateFilter

Convert a value to a string.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

class rmote.templates.filters.TemplateFilter[source]

Bases: object

Base for callable filter classes transferred with a Template.

Override __call__(self, value, ...) and pass the class in the template’s filters mapping. Filters must be constructible without arguments; each render creates fresh instances. Classes and instances are pickleable, including classes defined inside functions. Pickle carries the class source and its filter base classes, so the receiver needs no user module.

Keep filter definitions self-contained: put imports inside the class or method, and constants on the class. Enclosing variables, module globals and external decorators are not captured. Source must be in a readable file. Inheritance between filter classes is supported. Unpickling executes trusted Python code. Rendering invokes these explicitly registered filters.

Context containers are validated without copying. Filters receive the original values and are responsible for any mutations they perform.

Results are trusted values. Public attribute lookup and iterator consumption may execute result code; private attributes and method calls remain unavailable in the template language.

Set handles_undefined = True to receive TemplateUndefined for missing simple lookups, as the built-in default filter does.

__call__(value, *args, **kwargs)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

static attribute(value, attribute)[source]

Resolve a dotted path, preferring items to public object attributes.

Return type:

Any

handles_undefined = False
class rmote.templates.filters.TemplateUndefined(error)[source]

Bases: object

A missing filter result which only default may consume silently.

class rmote.templates.filters.ToJSONFilter[source]

Bases: TemplateFilter

Serialize as JSON with sorted keys and escaped HTML-sensitive characters.

__call__(value, indent=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Text

class rmote.templates.filters.TrimFilter[source]

Bases: TemplateFilter

Strip surrounding whitespace, or the supplied set of characters.

__call__(value, chars=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Text

class rmote.templates.filters.UniqueFilter[source]

Bases: TemplateFilter

Yield the first item for each distinct, hashable comparison key.

__call__(value, case_sensitive=False, attribute=None)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Iterator[Any]

class rmote.templates.filters.UpperFilter[source]

Bases: TemplateFilter

Convert to an uppercase string.

__call__(value)[source]

Transform the piped value with the supplied filter arguments.

Return type:

Any

rmote.templates.filters.TEMPLATE_FILTERS: Mapping[str, type[TemplateFilter]]

Default name-to-class mapping, used only when Template receives no filters mapping.