ReviewOS

stacks/stx

@foreach is not expanded server-side and {⁠{ }} is not interpolated in attributes — literal braces ship to the browser

#1842
Closed glennmichael123 opened this 22 days ago · 1 comment
22 days ago

packages/stx/docs/SCRIPT_VARIABLES.md:118 documents a server-side loop over a variable declared in a <script> block:

<script>
const items = [
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' },
]
</script>

@foreach(item in items)
  <div>{{ item.name }}</div>
@endforeach

In a Stacks app view (resources/views/*.stx extending a layout, markup inside @section('content')), neither half of that happens, and neither failure says anything.

Repro

@extends('layouts/postline')
@section('title', 'probe')
@section('content')
  <script>
    const probeProviders = [
      { id: 'bluesky', pascal: 'Bluesky', label: 'Bluesky' },
      { id: 'linkedin', pascal: 'Linkedin', label: 'LinkedIn' },
    ]
  </script>

  <section data-testid="probe">
    @foreach(p in probeProviders)
      <div data-variant-row="{⁠{ p.id }}">
        <textarea x-model="vText{⁠{ p.pascal }}" aria-label="{⁠{ p.label }} post body"></textarea>
      </div>
    @endforeach
  </section>
@endsection

Built output — one copy, braces intact:

<div data-variant-row="{⁠{ p.id }}" @for="p in probeProviders" x-cloak>
  <textarea x-model="vText{⁠{ p.pascal }}" aria-label="{⁠{ p.label }} post body">

Two separate problems:

  1. The loop is not expanded server-side. @foreach is rewritten into a client-side @for attribute on the element. The block appears once, not twice.
  2. {⁠{ }} is not interpolated inside attribute values. {⁠{ p.id }}, {⁠{ p.pascal }} and {⁠{ p.label }} ship verbatim to the browser.

With <script server> instead of a plain <script>, the @for rewrite does not happen either — the markup emits exactly once with all braces literal.

Why it matters

Both fail silently. Nothing warns at build time, the build reports success, and the page ships data-variant-row="{⁠{ p.id }}" — a literal that no selector will ever match and no binding will ever resolve. I only noticed because I inspected dist/ for the attribute I expected.

The {⁠{ }}-in-attribute case is the one I'd prioritise. Server-expanding a loop to write out N near-identical blocks is exactly the case where the values must land in attributes (data-*, aria-label, and for me an x-model signal name), so partial support does not help — and the literal braces reaching the browser is worse than an error.

Context

I hit this converting an imperative document.createElement panel to declarative markup: nine near-identical per-provider blocks that differ only by id, label and the bound signal name. The documented @foreach would have collapsed that to one block. Writing the nine out literally works, so this is not blocking me — but the documented feature does not behave as documented, and does so quietly.

If server-side expansion is not intended in a view inside @section('content'), the docs should say where it is intended, and an unexpanded {⁠{ … }} surviving into built output should probably be a build-time warning in its own right.

22 days ago

Fixed in 1c053f2.

It is one bug, not two

The two symptoms have a single cause: @foreach(item in items) was never implemented. Only Blade's items as item was.

{⁠{ }} interpolation inside attributes was never broken — the as form proves it:

@foreach(items as p)  ->  <div data-row="a">A</div><div data-row="b">B</div>   ✅
@foreach(p in items)  ->  @foreach(p in items)<div data-row=""></div>@endforeach   ❌

The braces survived because the loop never ran, so p never existed. Fixing the binding fixes both symptoms at once — including the x-model="vText{⁠{ p.pascal }}" signal-name case, which is the one you said mattered most.

The silence was the parser hitting an unrecognised binding and continue-ing, which leaves the directive text in place. Both spellings now go through one parser, since the operands are the other way round between them. (item, index) in items works too — it maps onto the comma form the item parser already understood.

Your docs point was right, and worse than you found

SCRIPT_VARIABLES.md was wrong in a second way, which is probably why you reached for a plain <script> in the first place: every example on that page spelled the block <script>, and a bare <script> is client-side in stx — only <script server> runs on the server. So none of those examples could feed a server directive even with the loop fixed.

All 18 occurrences are now <script server>, with the rule stated at the top of the page.

The build-time warning you asked for

an unexpanded {⁠{ … }} surviving into built output should probably be a build-time warning in its own right

A collection that is not server data emitted only an inline HTML comment — visible to someone already reading built output, which is exactly how you found this. It now also warns, and names the likely cause:

[stx] @foreach(items) in /app/login.stx: items is not iterable server-side, so the
loop rendered nothing. Values a server loop reads must come from <script server>
or the page context — a plain <script> runs in the browser.

Verification

Every shape from your repro, including inside @extends + @section('content'):

beforeafter
context variable1 row, directive literal2 rows
<script server>1 row, directive literal2 rows
same, inside @section1 row, directive literal2 rows
plain <script>silentexplicit error + build warning

Six of the eight behavioural tests fail at the parent commit; the two that pass are the as-form regression guards. Full suite 10789 pass in a clean worktree.

Sign in to comment on this issue.