Module

compiler.statements.functions

Function statement compilation for Kida compiler.

Provides mixin for compiling function statements (def, call_block, slot).

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

Classes

FunctionCompilationMixin 3
Mixin for compiling function statements. Host attributes and cross-mixin dependencies are declared…

Mixin for compiling function statements.

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

Methods

Internal Methods 3
_compile_def 1 list[ast.stmt]
Compile {% def name(args) %}...{% enddef %. Kida functions have true lexical s…
def _compile_def(self, node: Any) -> list[ast.stmt]

Compile {% def name(args) %}...{% enddef %.

Kida functions have true lexical scoping - they can access variables from their enclosing scope, unlike Jinja2 macros.

Generates:

def _def_name(arg1, arg2=default, *, _caller=None, _outer_ctx=ctx):
    buf = []
    ctx = {**_outer_ctx, 'arg1': arg1, 'arg2': arg2}
    if _caller:
        ctx['caller'] = _caller
    ... body ...
    return Markup(''.join(buf))
ctx['name'] = _def_name
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_call_block 1 list[ast.stmt]
Compile {% call func(args) %}body{% endcall %. Calls a function with the body …
def _compile_call_block(self, node: Any) -> list[ast.stmt]

Compile {% call func(args) %}body{% endcall %.

Calls a function with the body content as the caller.

Generates:

def _caller():
    buf = []
    ... body ...
    return Markup(''.join(buf))
_append(func(args, _caller=_caller))
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_slot 1 list[ast.stmt]
Compile {% slot %. Renders the caller content inside a {% def %}. Generates: …
def _compile_slot(self, node: Any) -> list[ast.stmt]

Compile {% slot %.

Renders the caller content inside a {% def %}.

Generates:

if ctx.get('caller'):
    _append(ctx['caller']())
Parameters
Name Type Description
node
Returns
list[ast.stmt]