{"record":{"id":"5d8de4e927a3322a","repo":"twigphp/Twig","slug":"cannot-use-extend-in-a-block","errorCode":null,"errorMessage":"Cannot use \"extend\" in a block.","messagePattern":"Cannot use \"extend\" in a block\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/NodeVisitor/CorrectnessNodeVisitor.php","lineNumber":190,"sourceCode":"        }\n    }\n\n    private function checkConfigTag(ConfigNode $node): void\n    {\n        if ('extends' === $node->getNodeTag()) {\n            $this->checkExtendsTag($node);\n        }\n\n        if (!isset($this->rootNodes[$node])) {\n            trigger_deprecation('twig/twig', '3.28', 'Using the \"%s\" tag outside the root of a template is deprecated in %s at line %d.', $node->getNodeTag(), $node->getSourceContext()->getName(), $node->getTemplateLine());\n        }\n    }\n\n    private function checkExtendsTag(ConfigNode $node): void\n    {\n        // \"extends\" inside a \"block\" or a \"macro\" has always been a hard error; keep it\n        if ($this->blockDepth) {\n            throw new SyntaxError('Cannot use \"extend\" in a block.', $node->getTemplateLine(), $node->getSourceContext());\n        }\n        if ($this->macroDepth) {\n            throw new SyntaxError('Cannot use \"extend\" in a macro.', $node->getTemplateLine(), $node->getSourceContext());\n        }\n        if ($this->hasExtends) {\n            throw new SyntaxError('Multiple extends tags are forbidden.', $node->getTemplateLine(), $node->getSourceContext());\n        }\n\n        $this->hasExtends = true;\n    }\n\n    private function checkBlockDefinitionNesting(BlockReferenceNode $node): void\n    {\n        // A \"block\" definition nested under an output-wrapping tag is registered globally\n        // regardless of that tag, so the nesting is misleading. This only matters at the root\n        // of a child template's body: once inside a block, a macro, an output capture, or in\n        // a standalone template, the block is rendered in place and behaves like any other.\n        if (!$this->hasParent || $this->blockDepth || $this->macroDepth || $this->capturingNodeDepth || !$this->tagStack) {","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/NodeVisitor/CorrectnessNodeVisitor.php#L172-L208","documentation":"Twig forbids placing an {% extends %} tag inside a {% block %}. A block is rendered within the child/parent rendering flow, and inheritance must be decided before any block is rendered, so checkExtendsTag throws a SyntaxError when blockDepth is non-zero.","triggerScenarios":"An {% extends %} tag nested inside a {% block %} ... {% endblock %} body; detected in checkExtendsTag (called from checkConfigTag) when $this->blockDepth > 0.","commonSituations":"Accidentally indenting {% extends %} inside a block during refactoring; generating templates programmatically and wrapping everything in a block; copy-pasting a whole template body (including its extends) into a block.","solutions":["Move {% extends %} to the very top of the template, outside any block","Remove the extends if the template should be standalone","Split into two templates: one that extends, one that only defines the block content"],"exampleFix":"// before\n{% block content %}\n  {% extends 'base.html.twig' %}\n{% endblock %}\n// after\n{% extends 'base.html.twig' %}\n{% block content %}...{% endblock %}","handlingStrategy":"validation","validationCode":"if (preg_match('/{%-?\\\\s*extends\\\\b/', $blockBody)) { throw new \\InvalidArgumentException('extends not allowed inside block'); }","typeGuard":null,"tryCatchPattern":"try { $env->render($name); } catch (\\Twig\\Error\\SyntaxError $e) { /* 'extend' placement error */ }","preventionTips":["{% extends %} must be top-level and first in a template","Never wrap whole templates in blocks","Validate template sources before deployment"],"tags":["twig","template-inheritance","syntax-error"],"backgroundTag":"invalid-template-structure","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"}