Module

compiler.statements.special_blocks

Special block statement compilation for Kida compiler.

Provides mixin for compiling special block statements (with, raw, capture, cache, filter_block).

Uses inline TYPE_CHECKING declarations for host attributes. See: plan/rfc-mixin-protocol-typing.md

Classes

SpecialBlockMixin 8
Mixin for compiling special block statements. Host attributes and cross-mixin dependencies are dec…

Mixin for compiling special block statements.

Host attributes and cross-mixin dependencies are declared via inline TYPE_CHECKING blocks.

Methods

Internal Methods 8
_compile_with 1 list[ast.stmt]
Compile {% with var=value, ... %}...{% endwith %. Creates temporary variable b…
def _compile_with(self, node: Any) -> list[ast.stmt]

Compile {% with var=value, ... %}...{% endwith %.

Creates temporary variable bindings scoped to the with block. We store old values and restore them after the block.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_with_conditional 1 list[ast.stmt]
Compile {% with expr as target %}...{% end %} (conditional form). Renders body…
def _compile_with_conditional(self, node: Any) -> list[ast.stmt]

Compile {% with expr as target %}...{% end %} (conditional form).

Renders body only if expr is truthy. Binds expr result to target. Supports multiple bindings and structural unpacking. Provides nil-resilience: block is silently skipped when expr is falsy.

Generates:

_with_val_N = expr
if _with_val_N:
    # [save old values]
    # [bind new values]
    ... body ...
    # [restore old values]
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_raw 1 list[ast.stmt]
Compile {% raw %}...{% endraw %. Raw block content is output as literal text.
def _compile_raw(self, node: Any) -> list[ast.stmt]

Compile {% raw %}...{% endraw %.

Raw block content is output as literal text.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_capture 1 list[ast.stmt]
Compile {% capture x %}...{% end %} (Kida) or {% set x %}...{% endset %} (Jinja…
def _compile_capture(self, node: Any) -> list[ast.stmt]

Compile {% capture x %}...{% end %} (Kida) or {% set x %}...{% endset %} (Jinja).

Captures rendered block content into a variable.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_cache 1 list[ast.stmt]
Compile {% cache key %}...{% endcache %. Fragment caching for expensive templa…
def _compile_cache(self, node: Any) -> list[ast.stmt]

Compile {% cache key %}...{% endcache %.

Fragment caching for expensive template sections.

Generates:

_cache_key = str(key)
_cached = _cache_get(_cache_key)
if _cached is not None:
    _append(_cached)
else:
    _cache_buf = []
    _cache_append = _cache_buf.append
    _save_append = _append
    _append = _cache_append
    ... body ...
    _append = _save_append
    _cached = ''.join(_cache_buf)
    _cache_set(_cache_key, _cached, ttl)
    _append(_cached)
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_filter_block 1 list[ast.stmt]
Compile {% filter name %}...{% endfilter %. Apply a filter to an entire block …
def _compile_filter_block(self, node: Any) -> list[ast.stmt]

Compile {% filter name %}...{% endfilter %.

Apply a filter to an entire block of content.

Uses unique variable names to support nesting.

Generates:

_filter_buf_N = []
_filter_append_N = _filter_buf_N.append
_save_append_N = _append
_append = _filter_append_N
... body ...
_append = _save_append_N
_append(_filters['name'](''.join(_filter_buf_N), *args, **kwargs))
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_spaceless 1 list[ast.stmt]
Compile {% spaceless %}...{% end %}. Removes whitespace between HTML tags. Par…
def _compile_spaceless(self, node: Any) -> list[ast.stmt]

Compile {% spaceless %}...{% end %}.

Removes whitespace between HTML tags. Part of RFC: kida-modern-syntax-features.

Generates:

_spaceless_buf_N = []
_spaceless_append_N = _spaceless_buf_N.append
_save_append_N = _append
_append = _spaceless_append_N
... body ...
_append = _save_append_N
_append(_spaceless(''.join(_spaceless_buf_N)))
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_embed 1 list[ast.stmt]
Compile {% embed 'template.html' %}...{% end %}. Embed is like include but all…
def _compile_embed(self, node: Any) -> list[ast.stmt]

Compile {% embed 'template.html' %}...{% end %}.

Embed is like include but allows block overrides. Part of RFC: kida-modern-syntax-features.

Generates:

_saved_blocks_N = _blocks.copy()
def _block_name(ctx, _blocks): ...  # For each override
_blocks['name'] = _block_name
_append(_include(template, ctx, _blocks))
_blocks = _saved_blocks_N
Parameters
Name Type Description
node
Returns
list[ast.stmt]