{"record":{"id":"4507d3e6043de753","repo":"twigphp/Twig","slug":"block-s-is-not-defined-in-trait-s","errorCode":null,"errorMessage":"Block \"%s\" is not defined in trait \"%s\".","messagePattern":"Block \"(.+?)\" is not defined in trait \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Node/ModuleNode.php","lineNumber":252,"sourceCode":"                    ->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(', ')\n                        ->subcompile($trait->getNode('template'))\n                        ->raw('), ')\n                        ->repr($node->getTemplateLine())\n                        ->raw(\", \\$this->source);\\n\")\n                        ->outdent()\n                        ->write(\"}\\n\\n\")\n\n                        ->write(\\sprintf('$_trait_%s_blocks[', $i))\n                        ->subcompile($value)\n                        ->raw(\\sprintf('] = $_trait_%s_blocks[', $i))\n                        ->string($key)\n                        ->raw(\\sprintf(']; unset($_trait_%s_blocks[', $i))\n                        ->string($key)\n                        ->raw(']); $this->traitAliases[')\n                        ->subcompile($value)\n                        ->raw('] = ')","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/ModuleNode.php#L234-L270","documentation":"When compiling a template that uses another template as a trait, Twig emits runtime checks that each requested block exists in the trait's block table ($_trait_N_blocks). A referenced block key missing from the trait template raises this RuntimeError at runtime.","triggerScenarios":"{% use 'trait.html' with { footer: 'pageFooter' } %} (or implicit same-name use) where trait.html does not define the requested block.","commonSituations":"Renaming a block in the trait template without updating the using template; alias targets misspelled; using a trait file from an older version.","solutions":["Define the missing block in the trait template","Fix the block key/alias in the {% use %} statement to match an existing block","Verify you are loading the intended trait template version"],"exampleFix":"// before\n{% use 'blocks.html' with { content: 'body' } %} {# blocks.html has no 'body' block #}\n// after\n{% use 'blocks.html' with { content: 'content' } %}","handlingStrategy":"validation","validationCode":"// Verify every used block exists in the trait template\npreg_match_all('/{%\\\\s*block\\\\s+(\\\\w+)/', file_get_contents($traitPath), $m);\n$defined = $m[1];\n$needed = ['content', 'footer'];\n$missing = array_diff($needed, $defined);\nif ($missing) throw new RuntimeException('Missing blocks: '.implode(',', $missing));","typeGuard":null,"tryCatchPattern":"try {\n    $twig->render($template, $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'is not defined in trait')) {\n        // fix block name or define it in the trait\n    }\n    throw $e;\n}","preventionTips":["Keep trait templates block-complete and documented","Update {% use %} aliases when renaming blocks","Add a compile/render smoke test per template"],"tags":["twig","template-inheritance","trait","blocks"],"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"}