{"record":{"id":"83caba9faf536485","repo":"twigphp/Twig","slug":"template-s-cannot-be-used-as-a-trait","errorCode":null,"errorMessage":"Template \"%s\" cannot be used as a trait.","messagePattern":"Template \"(.+?)\" cannot be used as a trait\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Node/ModuleNode.php","lineNumber":236,"sourceCode":"        }\n\n        $countTraits = \\count($this->getNode('traits'));\n        if ($countTraits) {\n            $compiler->write(\"\\$this->ensureTraitsAllowed();\\n\\n\");\n\n            foreach ($this->getNode('traits') as $i => $trait) {\n                $node = $trait->getNode('template');\n\n                $compiler\n                    ->addDebugInfo($node)\n                    ->write(\\sprintf('$_trait_%s = $this->load(', $i))\n                    ->subcompile($node)\n                    ->raw(', ')\n                    ->repr($node->getTemplateLine())\n                    ->raw(\");\\n\")\n                    ->write(\\sprintf(\"if (!\\$_trait_%s->unwrap()->isTraitable()) {\\n\", $i))\n                    ->indent()\n                    ->write(\"throw new RuntimeError('Template \\\"'.\")\n                    ->subcompile($trait->getNode('template'))\n                    ->raw(\".'\\\" cannot be used as a trait.', \")\n                    ->repr($node->getTemplateLine())\n                    ->raw(\", \\$this->source);\\n\")\n                    ->outdent()\n                    ->write(\"}\\n\")\n                    ->write(\\sprintf(\"\\$_trait_%s_blocks = \\$_trait_%s->unwrap()->getBlocks();\\n\\n\", $i, $i))\n                ;\n\n                foreach ($trait->getNode('targets') as $key => $value) {\n                    $compiler\n                        ->write(\\sprintf('if (!isset($_trait_%s_blocks[', $i))\n                        ->string($key)\n                        ->raw(\"])) {\\n\")\n                        ->indent()\n                        ->write(\"throw new RuntimeError(sprintf('Block \\\"%s\\\" is not defined in trait \\\"%s\\\".', \")\n                        ->string($key)\n                        ->raw(', ')","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/ModuleNode.php#L218-L254","documentation":"In {% extends %}/{% use %}-style trait compilation, ModuleNode emits runtime code that loads the trait template and checks isTraitable(). Templates without a block structure (e.g. plain text or templates whose top-level nodes are not blocks) are not traitable and the compiled code throws this RuntimeError at runtime.","triggerScenarios":"{% extends 'plain.html' %} where plain.html contains no {% block %} tags, or {% use 'file' %} pointing at a non-block template.","commonSituations":"Extending a partial/partial-like include by mistake; pointing 'use' at an HTML fragment instead of a block-only template.","solutions":["Ensure the parent/trait template is composed of {% block %} tags","If you meant to include content, use {% include %} instead of {% extends %}/{% use %}","Verify the template name resolves to the intended file"],"exampleFix":"// before\n{% use 'partials/_fragment.html' %}\n// after\n{% use 'traits/_form_blocks.html' %} {# contains only {% block %}s #}","handlingStrategy":"try-catch","validationCode":"// Pre-check traitability by ensuring the file defines blocks\n$src = file_get_contents($traitPath);\nif (!preg_match('/{%\\\\s*block\\\\s+\\\\w+/', $src)) {\n    throw new InvalidArgumentException(\"$traitPath is not a traitable template (no blocks)\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $twig->render($template, $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'cannot be used as a trait')) {\n        // point extends/use at a block-only template\n    }\n    throw $e;\n}","preventionTips":["Only extends/use templates made of blocks","Use {% include %} for fragments/partials","Verify template names resolve to intended files"],"tags":["twig","template-inheritance","trait","runtime"],"backgroundTag":"resource-not-found","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}