{"record":{"id":"f861eefba35c4a68","repo":"octobercms/october","slug":"cannot-capture-slot-name-no-active-component","errorCode":null,"errorMessage":"Cannot capture slot '{$name}': no active component.","messagePattern":"Cannot capture slot '(.+?)': no active component\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"modules/system/classes/ViewComponent.php","lineNumber":80,"sourceCode":"    /**\n     * slot starts capturing content for a named slot\n     */\n    public function slot(string $name = 'default'): void\n    {\n        ob_start();\n        $this->currentSlot = $name;\n        static::$stack[] = $this;\n    }\n\n    /**\n     * captureSlot switches to a new named slot, closing the previous one\n     */\n    public static function captureSlot(string $name): void\n    {\n        $instance = end(static::$stack);\n\n        if ($instance === false) {\n            throw new \\RuntimeException(\"Cannot capture slot '{$name}': no active component.\");\n        }\n\n        // Close previous slot\n        if ($instance->currentSlot !== null) {\n            $instance->slots[$instance->currentSlot] = ob_get_clean();\n        }\n\n        // Start new slot\n        ob_start();\n        $instance->currentSlot = $name;\n    }\n\n    /**\n     * endComponent captures the final slot and renders the component\n     */\n    public static function endComponent(): void\n    {\n        $instance = array_pop(static::$stack);","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/octobercms/october/blob/b608633a7e8922487d91a8161499020121c3b3bf/modules/system/classes/ViewComponent.php#L62-L98","documentation":"ViewComponent powers the UiFactory fluent UI API: `UiFactory::card(...)->slot()` pushes a component onto a static stack and opens an output buffer; captureSlot('name') then switches the captured slot. captureSlot() uses end(static::$stack) and throws a RuntimeException when the stack is empty, because a slot has to belong to some active component. So this error means captureSlot() ran before any ->slot() started a component, or after endComponent() already closed the last one.","triggerScenarios":"Calling ViewComponent::captureSlot('x') / UiFactory::captureSlot('x') in a view without a preceding `->slot()` call on a component; calling it after `UiFactory::end()` closed the only component; copy-pasting a slot block outside its component wrapper.","commonSituations":"Refactoring a UI template and moving a captureSlot block out of its component; nesting mistakes where the opening `->slot()` line was deleted; using conditional rendering that skips the open but keeps the captureSlot.","solutions":["Start a component first: `UiFactory::card(...)->slot()` (or equivalent), then captureSlot('name'), then UiFactory::end()","Check the template for a deleted or commented-out `->slot()` opening line above the captureSlot call","Make sure conditionals wrap whole open…close blocks, never just the opening half","Match every UiFactory::end() with exactly one ->slot() start in the same branch"],"exampleFix":"// before — captureSlot with no component opened\n<?php UiFactory::captureSlot('header') ?>\n    <h1>Hi</h1>\n<?php UiFactory::end() ?>\n// after — open the component (and default slot) first\n<?php UiFactory::card(title: 'Panel')->slot() ?>\n    <p>Body</p>\n    <?php UiFactory::captureSlot('header') ?>\n        <h1>Hi</h1>\n    <?php UiFactory::captureSlot('footer') ?>\n        <small>Bye</small>\n<?php UiFactory::end() ?>","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function hasActiveViewComponent(): bool\n{\n    $prop = new ReflectionProperty(\\System\\Classes\\ViewComponent::class, 'stack');\n    return !empty($prop->getValue());\n}\n\n// UiFactory::captureSlot('header') only when a component is open:\nif (hasActiveViewComponent()) {\n    \\System\\Classes\\ViewComponent::captureSlot('header');\n}","tryCatchPattern":null,"preventionTips":["Treat ->slot()/captureSlot()/UiFactory::end() like open/close tags: pair them 1:1 in every branch","Use linters/templates that highlight unbalanced component blocks","Prefer inline `<?= UiFactory::x(...) ?>` rendering when you don't need slots"],"tags":["octobercms","view","component","slot","output-buffer"],"backgroundTag":"unbalanced-view-nesting","analyzedSha":"b608633a7e8922487d91a8161499020121c3b3bf","analyzedAt":"2026-08-21T04:24:57.515Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}