Module

rendering.jinja_utils

Jinja2 utility functions for template development.

Provides helpers for working with Jinja2's Undefined objects and accessing template context safely.

Functions

is_undefined
Check if a value is a Jinja2 Undefined object. This is a wrapper around jinja2.is_undefined() that…
1 bool
def is_undefined(value: Any) -> bool

Check if a value is a Jinja2 Undefined object.

This is a wrapper around jinja2.is_undefined() that provides a clean API for template function developers.

Parameters 1

Name Type Default Description
value Any

Value to check

Returns

bool

True if value is Undefined, False otherwise

safe_get
Safely get attribute from object, handling Jinja2 Undefined values. This is a replacement for hasa…
3 Any
def safe_get(obj: Any, attr: str, default: Any = None) -> Any

Safely get attribute from object, handling Jinja2 Undefined values.

This is a replacement for hasattr()/getattr() that also handles Jinja2's Undefined objects and returns default for missing attributes, even when getattr is implemented.

Parameters 3

Name Type Default Description
obj Any

Object to get attribute from

attr str

Attribute name

default Any None

Default value if undefined or missing

Returns

Any

Attribute value or default

has_value
Check if value is defined and not None/empty. More strict than is_undefined() - also checks for No…
1 bool
def has_value(value: Any) -> bool

Check if value is defined and not None/empty.

More strict than is_undefined() - also checks for None and falsy values. Returns False for: Undefined, None, False, 0, "", [], {}

Parameters 1

Name Type Default Description
value Any

Value to check

Returns

bool

True if value is defined and truthy

safe_get_attr
Safely get nested attribute from object using dot notation.
1 Any
def safe_get_attr(obj: Any, *attrs: str) -> Any

Safely get nested attribute from object using dot notation.

Parameters 1

Name Type Default Description
obj Any

Object to get attribute from *attrs: Attribute names (can be nested)

Returns

Any

Final attribute value or default

ensure_defined
Ensure value is defined and not None, replacing Undefined/None with default. This is useful in tem…
2 Any
def ensure_defined(value: Any, default: Any = '') -> Any

Ensure value is defined and not None, replacing Undefined/None with default.

This is useful in templates to ensure a value is always usable, even if it's missing or explicitly set to None.

Parameters 2

Name Type Default Description
value Any

Value to check

default Any ''

Default value to use if undefined or None (default: "")

Returns

Any

Original value if defined and not None, default otherwise