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>
@endforeachIn 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>
@endsectionBuilt 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:
- The loop is not expanded server-side.
@foreachis rewritten into a client-side@forattribute on the element. The block appears once, not twice. {{ }}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.