Module

compiler.statements.template_structure

Template structure statement compilation for Kida compiler.

Provides mixin for compiling template structure statements (block, include, from_import).

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

Classes

TemplateStructureMixin 8
Mixin for compiling template structure statements. Host attributes and cross-mixin dependencies ar…

Mixin for compiling template structure statements.

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

Methods

Internal Methods 8
_yield_from_or_async_for 1 list[ast.stmt]
Generate yield from (sync) or async for + yield (async). In sync streaming mod…
def _yield_from_or_async_for(self, call_expr: ast.expr) -> list[ast.stmt]

Generate yield from (sync) or async for + yield (async).

In sync streaming mode: yield from call_expr In async streaming mode: async for _chunk in call_expr: yield _chunk

Python doesn't allow yield from inside async def, so async mode must use async for + yield instead.

Parameters
Name Type Description
call_expr
Returns
list[ast.stmt]
_compile_globals 1 list[ast.stmt]
Compile {% globals %}...{% end %} inline during full render. During render(), …
def _compile_globals(self, node: Globals) -> list[ast.stmt]

Compile {% globals %}...{% end %} inline during full render.

During render(), the globals body is compiled transparently — its statements execute as part of the normal template flow. The separate _globals_setup function for render_block() is handled by _compile_template.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_imports 1 list[ast.stmt]
Compile {% imports %}...{% end %} inline during full render. Same as {% global…
def _compile_imports(self, node: Imports) -> list[ast.stmt]

Compile {% imports %}...{% end %} inline during full render.

Same as {% globals %} — body runs during render(). _globals_setup for render_block() is handled by _compile_template.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_block 1 list[ast.stmt]
Compile {% block name [if cond] %} ... {% endblock %. StringBuilder: _append(_…
def _compile_block(self, node: Block) -> list[ast.stmt]

Compile {% block name [if cond] %} ... {% endblock %.

StringBuilder: _append(_blocks.get('name', _block_name)(ctx, _blocks)) Streaming: yield from _blocks.get('name', _block_name_stream)(ctx, _blocks) Async streaming: async for _chunk in _blocks.get(...): yield _chunk

Fragment blocks (node.fragment=True) are skipped entirely during full template render — they emit no output. The block function is still compiled and accessible via render_block().

Conditional blocks (node.condition is not None) wrap the block output inif <cond>:, so the block is only rendered when the condition is truthy.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_wrap_block_condition 2 list[ast.stmt]
Wrap block statements in ``if :`` when a guard is present. If ``node.condition…
def _wrap_block_condition(self, node: Block, stmts: list[ast.stmt]) -> list[ast.stmt]

Wrap block statements inif <cond>:when a guard is present.

Ifnode.conditionis None, returns stmts unchanged.

Parameters
Name Type Description
node
stmts
Returns
list[ast.stmt]
_compile_include 1 list[ast.stmt]
Compile {% include "template.html" [with context] %. StringBuilder: _append(_i…
def _compile_include(self, node: Include) -> list[ast.stmt]

Compile {% include "template.html" [with context] %.

StringBuilder: _append(_include(template_name, ctx)) Streaming: yield from _include_stream(template_name, ctx)

When with_context is true and there are active loop variables or scope-stack variables, generates a merged context dict so that loop variables (Python locals) and {% set %} variables (scope stack) are visible inside the included template.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_from_import 1 list[ast.stmt]
Compile {% from "template.html" import name1, name2 as alias %. Generates: …
def _compile_from_import(self, node: FromImport) -> list[ast.stmt]

Compile {% from "template.html" import name1, name2 as alias %.

Generates:

_imported = _import_macros(template_name, with_context, ctx)
ctx['name1'] = _imported['name1']
ctx['alias'] = _imported['name2']
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_import 1 list[ast.stmt]
Compile {% import "template.html" as f %. Generates: ctx['f'] = _import_macros…
def _compile_import(self, node: Import) -> list[ast.stmt]

Compile {% import "template.html" as f %.

Generates: ctx['f'] = _import_macros(template_name, with_context, ctx)

Parameters
Name Type Description
node
Returns
list[ast.stmt]