Skip to content

Raise a helpful error for {{ index }} outside an items expansion (fixes #186)#232

Merged
fcsonline merged 1 commit into
masterfrom
fix/186-index-helpful-error
Jun 1, 2026
Merged

Raise a helpful error for {{ index }} outside an items expansion (fixes #186)#232
fcsonline merged 1 commit into
masterfrom
fix/186-index-helpful-error

Conversation

@fcsonline

@fcsonline fcsonline commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Fixes #186.

Problem

{{ index }} is only seeded into the context inside with_items, with_items_range, with_items_from_csv and with_items_from_file requests, where it is the item's position in the list. Referencing it anywhere else (e.g. a plain request, or an assert on index) failed with a generic, unhelpful message:

  • panic in the default strict mode: Unknown 'index' variable!
  • a bare warning under --relaxed-interpolations

Neither gave any clue that index is scope-limited, and the user almost always actually wanted the iteration counter — which already exists as iteration.

Fix

Rather than overloading index with a second meaning, detect this specific case and raise a dedicated message that explains the variable's scope and points to iteration:

Unknown 'index' variable! It is only available inside 'with_items',
'with_items_range', 'with_items_from_csv' or 'with_items_from_file' requests.
Use 'iteration' for the current iteration number.

The message is used both for the strict-mode panic and the relaxed-mode warning. with_items* requests are unaffected — index still resolves there as before.

Docs

Documented the built-in interpolation variables (base, iteration, item, index) and index's scope in SYNTAX.md.

Verification

  • Issue {{ index }} not working #186 repro now fails with the helpful message instead of Unknown 'index' variable!.
  • A with_items request still resolves {{ index }} (e.g. /a/0, /b/1).
  • Relaxed mode prints the helpful warning and continues.
  • New unit test index_outside_expansion_suggests_iteration. cargo fmt, cargo clippy -- -D warnings, cargo test (62) all pass.

…ixes #186)

`index` is only seeded inside with_items / with_items_range /
with_items_from_csv / with_items_from_file requests. Referencing it elsewhere
panicked (or warned under --relaxed-interpolations) with a generic
"Unknown 'index' variable" message that gave no clue about the cause.

Detect that specific case and raise a dedicated message explaining the
variable's scope and suggesting `iteration` for the current iteration number.
Also document the built-in interpolation variables in SYNTAX.md.
@fcsonline fcsonline merged commit 8fd9bf1 into master Jun 1, 2026
9 checks passed
@fcsonline fcsonline mentioned this pull request Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

{{ index }} not working

1 participant