Skip to content

Support else blocks in for loops - #1350

Open
alloutflo wants to merge 1 commit into
HubSpot:masterfrom
alloutflo:fix/1061-for-else
Open

alloutflo wants to merge 1 commit into
HubSpot:masterfrom
alloutflo:fix/1061-for-else

Conversation

@alloutflo

Copy link
Copy Markdown

Fixes #1061.

{% for user in users %}{{ user }}{% else %}EMPTY{% endfor %} currently renders an empty string for [] and aEMPTYbEMPTY for ['a', 'b']. This change renders EMPTY and ab, respectively.

Split the direct else child from the loop body and render it once when the collection is empty, in its own child scope. Nested branches, outer loop variables, and existing break/continue behavior are preserved. Validation still checks both branches without emitting inactive output or applying its mutations. Eager rendering retains both alternatives for unresolved collections and isolates their speculative state changes before reconstruction. No public APIs, configuration options, or dependencies are added.

Validation:

  • Regression tests failed on the original implementation for empty/nonempty collections and deferred reconstruction, then passed with this change.
  • 112 focused tests pass, including nested loops, null/empty collections, arrays and iterators, multiple loop variables, scope and side effects, strict unknown variables, whitespace/escaping, output limits, and direct-versus-two-pass rendering.
  • Full mvn --batch-mode -no-transfer-progress -V -Dproject.build.jdk.version=<17|21|25> verify passes on Temurin 17, 21, and 25, with Maven running on Java 25: 2,269 tests per run, zero failures/errors, 23 skipped. Java 21 and 25 were checked in fresh checkouts of the same commit.
  • Runs use en_US to avoid the previously reproduced, unrelated locale-dependent number-format assertions. Formatting, static checks, and git diff --check pass.

Developed with AI assistance, including implementation, regression tests, and diff review.

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.

for else control structure doesn't seem to be supported

1 participant