Functions
is_undefined
Check if a value is a Jinja2 Undefined object.
This is a wrapper around jinja2.is_undefined() that…
is_undefined
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
True if value is Undefined, False otherwisebool
—
safe_get
Safely get attribute from object, handling Jinja2 Undefined values.
This is a replacement for hasa…
safe_get
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
Attribute value or defaultAny
—
has_value
Check if value is defined and not None/empty.
More strict than is_undefined() - also checks for No…
has_value
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
True if value is defined and truthybool
—
safe_get_attr
Safely get nested attribute from object using dot notation.
safe_get_attr
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
Final attribute value or defaultAny
—
ensure_defined
Ensure value is defined and not None, replacing Undefined/None with default.
This is useful in tem…
ensure_defined
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
Original value if defined and not None, default otherwiseAny
—