Module

templating.filters

Built-in chirp template filters.

These are web-framework-specific filters auto-registered on every chirp kida Environment. They complement Kida's built-in filters with patterns common in server-rendered HTML + htmx apps.

Functions

bem 4 str
Build chirpui BEM class string: chirpui-{block} chirpui-{block}--{variant} etc.
def bem(block: str, variant: str = '', modifier: str = '', cls: str = '') -> str

Build chirpui BEM class string: chirpui-{block} chirpui-{block}--{variant} etc.

Parameters
Name Type Description
block str
variant str Default:''
modifier str Default:''
cls str Default:''
Returns
str
attr 2 str | Markup
Output an HTML attribute when value is truthy, else empty string. Shorthand fo…
def attr(value: Any, name: str) -> str | Markup

Output an HTML attribute when value is truthy, else empty string.

Shorthand for optional attributes without{% if %}blocks.

Parameters
Name Type Description
value Any
name str
Returns
str | Markup
_serialize_attr_value 1 str
Serialize attribute value into a stable string.
def _serialize_attr_value(value: Any) -> str
Parameters
Name Type Description
value Any
Returns
str
html_attrs 1 str | Markup
Render HTML attributes from mapping or legacy raw string. Contract: - ``dict``…
def html_attrs(value: Any) -> str | Markup

Render HTML attributes from mapping or legacy raw string.

Contract:

  • dict / Mapping: escaped, deterministic HTML attributes
  • str / Markup: pass through (legacy compatibility)
  • None / False: no output

Mapping values follow HTML attribute semantics:

  • True renders as a valueless attribute (e.g. disabled)
  • False / Noneare omitted
  • other values are escaped and rendered askey="value"
Parameters
Name Type Description
value Any
Returns
str | Markup
field_errors 2 list[str]
Extract validation errors for a single form field. Safely navigates a ``{field…
def field_errors(errors: Any, field_name: str) -> list[str]

Extract validation errors for a single form field.

Safely navigates a{field: [messages]}dict, returning an empty list when errors is None, missing, or the field has no errors.

Parameters
Name Type Description
errors Any
field_name str
Returns
list[str]
qs 2 str
Append query-string parameters to a URL path. Omits parameters whose values ar…
def qs(base: str, **params: Any) -> str

Append query-string parameters to a URL path.

Omits parameters whose values are falsy (None, "", 0, False) so callers can pass optional filters without manual guards.

Parameters
Name Type Description
base str
**params Any
Returns
str
timeago 1 str
Convert a unix timestamp to a human-readable relative time.
def timeago(unix_ts: int | float) -> str
Parameters
Name Type Description
unix_ts int | float
Returns
str
pluralize 3 str
Pluralize a word based on count.
def pluralize(count: int, singular: str, plural: str | None = None) -> str
Parameters
Name Type Description
count int
singular str
plural str | None Default:None
Returns
str
format_time 1 str
Format a unix timestamp as ``HH:MM:SS`` (UTC).
def format_time(unix_ts: float) -> str
Parameters
Name Type Description
unix_ts float
Returns
str
url 2 str
Safelist URL for href attributes. Uses Kida's url_is_safe. Returns the URL if …
def url(value: str, fallback: str = '#') -> str

Safelist URL for href attributes. Uses Kida's url_is_safe.

Returns the URL if the scheme is safe (http, https, relative), otherwise returns fallback. Use when building href from user or external data.

Parameters
Name Type Description
value str
fallback str Default:'#'
Returns
str
island_props 1 Markup
Serialize a value for safe use in ``data-island-props``. Returns HTML-escaped …
def island_props(value: Any) -> Markup

Serialize a value for safe use indata-island-props.

Returns HTML-escaped JSON as Markup so templates can embed props without manual escaping:

<div data-island-props="{{ props | island_props }}"></div>
Parameters
Name Type Description
value Any
Returns
Markup
island_attrs 6 Markup
Build a safe island mount attribute string. Designed for framework-agnostic mo…
def island_attrs(name: str, props: Any | None = None, *, mount_id: str | None = None, version: str = '1', src: str | None = None, cls: str = '') -> Markup

Build a safe island mount attribute string.

Designed for framework-agnostic mount roots:

<div{{ island_attrs("editor", props=state, mount_id="editor-root") }}>
    ...
</div>
Parameters
Name Type Description
name str
props Any | None Default:None
mount_id str | None Default:None
version str Default:'1'
src str | None Default:None
cls str Default:''
Returns
Markup
primitive_attrs 6 Markup
Build island attributes with primitive metadata conventions.
def primitive_attrs(primitive: str, props: dict[str, Any] | None = None, *, mount_id: str | None = None, version: str = '1', src: str | None = None, cls: str = '') -> Markup
Parameters
Name Type Description
primitive str
props dict[str, Any] | None Default:None
mount_id str | None Default:None
version str Default:'1'
src str | None Default:None
cls str Default:''
Returns
Markup