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:
TemplateFilterCapitalize a string.
- class rmote.templates.filters.DefaultFilter[source]¶
Bases:
TemplateFilterReplace 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:
- handles_undefined = True¶
- class rmote.templates.filters.FirstFilter[source]¶
Bases:
TemplateFilterReturn the first item, or an undefined result for an empty iterable.
- class rmote.templates.filters.FloatFilter[source]¶
Bases:
TemplateFilterConvert to float, returning the fallback on a failed conversion.
- class rmote.templates.filters.IPAddressFilter[source]¶
Bases:
TemplateFilterParse an interface, optionally returning an IP operation as plain data.
Without
action, return theipaddress.ip_interface()object.actionisinterface,address,network,network_address,netmask,hostmask,broadcast,prefix, orversion. Prefix and version are integers; other results are strings. Parsing usesipaddress.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:
TemplateFilterConvert to int, accepting decimal float strings and a fallback value.
- class rmote.templates.filters.JoinFilter[source]¶
Bases:
TemplateFilterJoin stringified items, optionally selecting an attribute first.
- class rmote.templates.filters.LastFilter[source]¶
Bases:
TemplateFilterReturn the last item of a reversible sequence, or an undefined result.
- class rmote.templates.filters.LengthFilter[source]¶
Bases:
TemplateFilterReturn the length of a sized value.
- class rmote.templates.filters.ListFilter[source]¶
Bases:
TemplateFilterMaterialize an iterable as a list.
- class rmote.templates.filters.LowerFilter[source]¶
Bases:
TemplateFilterConvert to a lowercase string.
- class rmote.templates.filters.ReplaceFilter[source]¶
Bases:
TemplateFilterReplace occurrences after converting the value and replacements to strings.
- class rmote.templates.filters.ReverseFilter[source]¶
Bases:
TemplateFilterReverse a string or return an iterator over an iterable in reverse order.
- class rmote.templates.filters.SortFilter[source]¶
Bases:
TemplateFilterStable sort; strings compare case-insensitively unless requested otherwise.
- class rmote.templates.filters.StringFilter[source]¶
Bases:
TemplateFilterConvert a value to a string.
- class rmote.templates.filters.TemplateFilter[source]¶
Bases:
objectBase for callable filter classes transferred with a
Template.Override
__call__(self, value, ...)and pass the class in the template’sfiltersmapping. 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 = Trueto receiveTemplateUndefinedfor missing simple lookups, as the built-indefaultfilter does.- __call__(value, *args, **kwargs)[source]¶
Transform the piped value with the supplied filter arguments.
- Return type:
- static attribute(value, attribute)[source]¶
Resolve a dotted path, preferring items to public object attributes.
- Return type:
- handles_undefined = False¶
- class rmote.templates.filters.TemplateUndefined(error)[source]¶
Bases:
objectA missing filter result which only
defaultmay consume silently.
- class rmote.templates.filters.ToJSONFilter[source]¶
Bases:
TemplateFilterSerialize as JSON with sorted keys and escaped HTML-sensitive characters.
- class rmote.templates.filters.TrimFilter[source]¶
Bases:
TemplateFilterStrip surrounding whitespace, or the supplied set of characters.
- class rmote.templates.filters.UniqueFilter[source]¶
Bases:
TemplateFilterYield the first item for each distinct, hashable comparison key.
- class rmote.templates.filters.UpperFilter[source]¶
Bases:
TemplateFilterConvert to an uppercase string.
- rmote.templates.filters.TEMPLATE_FILTERS: Mapping[str, type[TemplateFilter]]¶
Default name-to-class mapping, used only when Template receives no filters mapping.