# loop_context

URL: /kida/api/template/loop_context/
Section: template
Description: Loop iteration metadata for Kida ``{% for %}`` and ``{% async for %}`` blocks.

---

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

Open LLM text
(/kida/api/template/loop_context/index.txt)

Share with AI

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

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

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

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

Module

#
`template.loop_context`

Loop iteration metadata for Kida`{% for %}` and `{% async for %}`blocks.

2Classes1Function

## Classes

`LoopContext`

13

▼

Loop iteration metadata accessible as `loop` inside `{% for %}` blocks.

Provides index tracking, b…

Loop iteration metadata accessible as`loop` inside `{% for %}`blocks.

Provides index tracking, boundary detection, and utility methods for
common iteration patterns. All properties are computed on-access.

Properties:
index: 1-based iteration count (1, 2, 3, ...)
index0: 0-based iteration count (0, 1, 2, ...)
first: True on the first iteration
last: True on the final iteration
length: Total number of items in the sequence
revindex: Reverse 1-based index (counts down to 1)
revindex0: Reverse 0-based index (counts down to 0)
previtem: Previous item in sequence (None on first)
nextitem: Next item in sequence (None on last)

Methods:
cycle(*values): Return values[index % len(values)]

Output:

```
        <ul>
            <li class="odd">1/3: Apple ← First</li>
            <li class="even">2/3: Banana</li>
            <li class="odd">3/3: Cherry ← Last</li>
        </ul>
```
```





#### Methods

















        `index`

0


          `int`


▼


1-based iteration count.





  property









      `def index(self) -> int`




##### Returns

      `int`















        `index0`

0


          `int`


▼


0-based iteration count.





  property









      `def index0(self) -> int`




##### Returns

      `int`















        `first`

0


          `bool`


▼


True if this is the first iteration.





  property









      `def first(self) -> bool`




##### Returns

      `bool`















        `last`

0


          `bool`


▼


True if this is the last iteration.





  property









      `def last(self) -> bool`




##### Returns

      `bool`















        `length`

0


          `int`


▼


Total number of items in the sequence.





  property









      `def length(self) -> int`




##### Returns

      `int`















        `revindex`

0


          `int`


▼


Reverse 1-based index (counts down to 1).





  property









      `def revindex(self) -> int`




##### Returns

      `int`















        `revindex0`

0


          `int`


▼


Reverse 0-based index (counts down to 0).





  property









      `def revindex0(self) -> int`




##### Returns

      `int`















        `previtem`

0


          `Any`


▼


Previous item in the sequence, or None if first.





  property









      `def previtem(self) -> Any`




##### Returns

      `Any`















        `nextitem`

0


          `Any`


▼


Next item in the sequence, or None if last.





  property









      `def nextitem(self) -> Any`




##### Returns

      `Any`















        `cycle`

1


          `Any`


▼


Cycle through the given values.









      `def cycle(self, *values: Any) -> Any`




##### Parameters




        Name
        Type
        Description





        `*values`
`—`











##### Returns

      `Any`











      Internal Methods
      3

    ▼













        `__init__`

1

▼










      `def __init__(self, items: list[Any]) -> None`




##### Parameters




        Name
        Type
        Description





        `items`
`—`





















        `__iter__`

0


          `Any`


▼


Iterate through items, updating index for each.









      `def __iter__(self) -> Any`




##### Returns

      `Any`















        `__repr__`

0


          `str`


▼










      `def __repr__(self) -> str`




##### Returns

      `str`

























        `AsyncLoopContext`

13

▼


Loop iteration metadata for ``{% async for %}`` blocks.

Provides index-forward variables only. Siz…










Loop iteration metadata for`{% async for %}`blocks.

Provides index-forward variables only. Size-dependent variables
(`last`, `length`, `revindex`, `revindex0`, `nextitem`)
raise`TemplateRuntimeError`because async iterables cannot be
pre-materialized without defeating the purpose of lazy streaming.

The async for-loop drives iteration externally — this class does not
implement`__aiter__`. Instead, `advance(item)`is called at the
top of each loop iteration by the compiled code.

Properties:


```
index: 1-based iteration count (1, 2, 3, ...)
index0: 0-based iteration count (0, 1, 2, ...)
first: True on the first iteration
previtem: Previous item in sequence (None on first)
```

Methods:


```
cycle(*values): Return values[index % len(values)]
advance(item): Update state for current iteration (called by compiled code)
```

Part of RFC: rfc-async-rendering.






#### Methods

















        `index`

0


          `int`


▼


1-based iteration count.





  property









      `def index(self) -> int`




##### Returns

      `int`















        `index0`

0


          `int`


▼


0-based iteration count.





  property









      `def index0(self) -> int`




##### Returns

      `int`















        `first`

0


          `bool`


▼


True if this is the first iteration.





  property









      `def first(self) -> bool`




##### Returns

      `bool`















        `last`

0


          `bool`


▼


Not available in async for-loops.





  property









      `def last(self) -> bool`




##### Returns

      `bool`















        `length`

0


          `int`


▼


Not available in async for-loops.





  property









      `def length(self) -> int`




##### Returns

      `int`















        `revindex`

0


          `int`


▼


Not available in async for-loops.





  property









      `def revindex(self) -> int`




##### Returns

      `int`















        `revindex0`

0


          `int`


▼


Not available in async for-loops.





  property









      `def revindex0(self) -> int`




##### Returns

      `int`















        `previtem`

0


          `Any`


▼


Previous item in the sequence, or None if first.





  property









      `def previtem(self) -> Any`




##### Returns

      `Any`















        `nextitem`

0


          `Any`


▼


Not available in async for-loops.





  property









      `def nextitem(self) -> Any`




##### Returns

      `Any`















        `advance`

1

▼


Update loop state for the current iteration.

Called by compiled async for-loop…









      `def advance(self, item: Any) -> None`




Update loop state for the current iteration.


Called by compiled async for-loop code at the top of each iteration,
  before the loop body executes.






##### Parameters




        Name
        Type
        Description





        `item`
`—`





















        `cycle`

1


          `Any`


▼


Cycle through the given values.









      `def cycle(self, *values: Any) -> Any`




##### Parameters




        Name
        Type
        Description





        `*values`
`—`











##### Returns

      `Any`











      Internal Methods
      2

    ▼













        `__init__`

0

▼










      `def __init__(self) -> None`














        `__repr__`

0


          `str`


▼










      `def __repr__(self) -> str`




##### Returns

      `str`




















## Functions











        `_async_loop_unavailable`

1


          `NoReturn`


▼


Raise TemplateRuntimeError for size-dependent loop variables in async for.









      `def _async_loop_unavailable(name: str) -> NoReturn`




##### Parameters




        Name
        Type
        Description





        `name`
`str`











##### Returns

      `NoReturn`
