ReviewOS

stacks/stx

@slot() nested inside a @section() block isn't resolved — renders as literal text

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

Summary

A @slot('name') directive placed inside a @section('name') block is never resolved — it gets merged into the layout verbatim and renders as the literal string @slot('name') on the page. This breaks the common "parent view as a nested-route layout" pattern, where a view holds a @slot('content') placeholder to receive its child/tab content.

Root cause (src/process.ts)

Two passes run, in this order, over the view output:

  1. Sections are extracted and removed from output (~line 677): the @section('name') … @endsection replace stores the body into a sections map and the callback return ''s — so the section body (and anything inside it) is pulled out of output.
  2. The slot resolver runs over output (~line 692):
    output = output.replace(/@(?:yield|slot)\(\s*['"]([^'"]+)['"](?:,\s*['"]([^'"]+)['"])?\)/g,
      (_, name, defaultContent) => sections[name] || defaultContent || '')

Because step 1 already removed section bodies from output, any @slot(…) that lived inside a section is no longer in output when step 2 runs — it's never resolved. When the section body is later injected into the layout (the @yield/@slot substitution around lines ~800–840), the nested @slot(…) is emitted as-is → literal text.

Note the resolver already collapses an unfilled slot to '' (sections[name] || defaultContent || ''), so the intended behavior for an empty slot is "render nothing." The bug is solely that slots nested in sections never reach the resolver.

Reproduction

A view that uses @slot inside its own @section('content') — e.g. a parent view acting as a nested-route / tab layout:

@extends('layouts/default')

@section('content')
  <h1>Profile</h1>
  <nav>…tabs…</nav>
  @slot('content')   {⁠{-- expected: child/tab content, or empty; actual: literal "@slot('content')" --}}
@endsection

The rendered HTML contains the literal text @slot('content').

Impact

Any "parent view as nested-route layout" leaks the directive. In our app, the judge and courthouse profile pages (judges/[id].stx, court-houses/[id].stx) hold a @slot('content') inside their @section('content') to render their /reviews, /cases, /profile tab children. On a tab route the child fills it; but on the base route (no child), the literal @slot('content') renders on the page.

Suggested fix

Resolve @yield/@slot inside section bodies too — either run the slot-resolution replace over each extracted section's content inside the line-~677 callback before storing it into sections, or re-run the resolver after sections are merged into the layout, so nested (and unfilled) slots collapse to their default or empty string.

Version

@stacksjs/stx 0.2.70

22 days ago

Fixed in main (commit pushed). Root cause was exactly as you diagnosed: sections are extracted from output before the slot resolver runs, so a @slot(…) nested inside a @section(…) never reached the resolver and leaked verbatim once the section was merged into the layout's @yield.

The fix adds a resolveNestedSlots() pass right after the section→layout merge (process.ts): it resolves any surviving @yield/@slot — filling from a sibling section when one exists, and collapsing a self-referential slot (a section body referencing its own name — @slot('content') inside @section('content'), which would recurse) or an unfilled slot to its default or empty string. So a nested slot renders its fill, its default, or nothing — never the literal text.

Tested via the src processDirectives directly (4 cases incl. your exact repro); confirmed the tests fail without the fix. Closing.

Sign in to comment on this issue.