{"record":{"id":"f135f9f18069fc60","repo":"twigphp/Twig","slug":"expected-endblock-for-block-s-but-s-given","errorCode":null,"errorMessage":"Expected endblock for block \"%s\" (but \"%s\" given).","messagePattern":"Expected endblock for block \"(.+?)\" \\(but \"(.+?)\" given\\)\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/TokenParser/BlockTokenParser.php","lineNumber":52,"sourceCode":"final class BlockTokenParser extends AbstractTokenParser\n{\n    public function parse(Token $token): Node\n    {\n        $lineno = $token->getLine();\n        $stream = $this->parser->getStream();\n        $name = $stream->expect(Token::NAME_TYPE)->getValue();\n        $this->parser->setBlock($name, $block = new BlockNode($name, new EmptyNode(), $lineno));\n        $this->parser->setDocumentationTarget($block);\n        $this->parser->pushLocalScope();\n        $this->parser->pushBlockStack($name);\n\n        if ($stream->nextIf(Token::BLOCK_END_TYPE)) {\n            $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);\n            if ($token = $stream->nextIf(Token::NAME_TYPE)) {\n                $value = $token->getValue();\n\n                if ($value != $name) {\n                    throw new SyntaxError(\\sprintf('Expected endblock for block \"%s\" (but \"%s\" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext());\n                }\n            }\n        } else {\n            $body = new Nodes([\n                new PrintNode($this->parser->parseExpression(), $lineno),\n            ]);\n        }\n        $stream->expect(Token::BLOCK_END_TYPE);\n\n        $block->setNode('body', $body);\n        $this->parser->popBlockStack();\n        $this->parser->popLocalScope();\n\n        return new BlockReferenceNode($name, $lineno);\n    }\n\n    public function decideBlockEnd(Token $token): bool\n    {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/TokenParser/BlockTokenParser.php#L34-L70","documentation":"Thrown by BlockTokenParser::parse when a {% block %} is closed with {% endblock name %} whose name does not match the opening block's name. Twig checks the optional name after endblock for consistency and raises a SyntaxError pointing at the endblock line. The mismatch almost always indicates a copy-paste or nesting mistake.","triggerScenarios":"Writing {% block title %}...{% endblock header %} — the NAME token after endblock differs from the opening block name (loose != comparison, so case differs count as mismatch only for different strings).","commonSituations":"Copy-pasting blocks and forgetting to update the endblock label; misreading nesting where an inner endblock closes the wrong block; automated refactors that renamed the opening tag only.","solutions":["Change the name after endblock to match the opening block name.","Remove the name entirely: plain {% endblock %} is always valid.","Check block nesting — the mismatched endblock may be closing the wrong block.","Rename the opening {% block %} if the endblock name is the intended one."],"exampleFix":"{% block title %}Hello{% endblock header %}\n\n{# after #}\n{% block title %}Hello{% endblock %}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $twig->parse($twig->tokenize(new \\Twig\\Source($code, $name)));\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'Expected endblock')) {\n        // surface template name/line in build output\n    }\n    throw $e;\n}","preventionTips":["Prefer plain {% endblock %} with no name label.","Enable editor linting for unbalanced/mismatched block tags.","When copy-pasting blocks, rename both opening and closing tags together."],"tags":["twig","block","syntax-error","template-parsing"],"backgroundTag":"invalid-argument-value","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"}