# with_blocks

URL: /kida/api/compiler/statements/with_blocks/
Section: statements
Description: With-block statement compilation for Kida compiler.

Provides mixin for compiling with/endwith statements including both the
simple form ({% with var=expr %}) and the conditional form ({% with expr as target %}).

Extracted from special_blocks.py for module focus (RFC: compiler decomposition).
Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/compiler/statements/with_blocks/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fwith_blocks%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fwith_blocks%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fwith_blocks%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fwith_blocks%2Findex.txt)

Module

#
`compiler.statements.with_blocks`

With-block statement compilation for Kida compiler.

Provides mixin for compiling with/endwith statements including both the
simple form ({% with var=expr %}) and the conditional form ({% with expr as target %}).

Extracted from special_blocks.py for module focus (RFC: compiler decomposition).
Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

1Class

## Classes

`WithBlockMixin`

2

▼

Mixin for compiling with-block statements.

Host attributes and cross-mixin dependencies are declar…

Mixin for compiling with-block statements.

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

#### Methods

Internal Methods
2

▼

`_compile_with`

1

`list[ast.stmt]`

▼

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

Creates temporary variable b…

`def _compile_with(self, node: With) -> 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: WithConditional) -> 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]`
