{"record":{"id":"f7e23557b437474c","repo":"twigphp/Twig","slug":"unexpected-end-of-template-twig-was-looking-for-the","errorCode":null,"errorMessage":"Unexpected end of template. Twig was looking for the following tags \"else\", \"elseif\", or \"endif\" to close the \"if\" block started at line %d).","messagePattern":"Unexpected end of template\\. Twig was looking for the following tags \"else\", \"elseif\", or \"endif\" to close the \"if\" block started at line (.+?)\\)\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/TokenParser/IfTokenParser.php","lineNumber":67,"sourceCode":"                case 'else':\n                    $stream->expect(Token::BLOCK_END_TYPE);\n                    $else = $this->parser->subparse([$this, 'decideIfEnd']);\n                    break;\n\n                case 'elseif':\n                    $expr = $this->parser->parseExpression();\n                    $stream->expect(Token::BLOCK_END_TYPE);\n                    $body = $this->parser->subparse([$this, 'decideIfFork']);\n                    $tests[] = $expr;\n                    $tests[] = $body;\n                    break;\n\n                case 'endif':\n                    $end = true;\n                    break;\n\n                default:\n                    throw new SyntaxError(\\sprintf('Unexpected end of template. Twig was looking for the following tags \"else\", \"elseif\", or \"endif\" to close the \"if\" block started at line %d).', $lineno), $stream->getCurrent()->getLine(), $stream->getSourceContext());\n            }\n        }\n\n        $stream->expect(Token::BLOCK_END_TYPE);\n\n        return new IfNode(new Nodes($tests), $else, $lineno);\n    }\n\n    public function decideIfFork(Token $token): bool\n    {\n        return $token->test(['elseif', 'else', 'endif']);\n    }\n\n    public function decideIfEnd(Token $token): bool\n    {\n        return $token->test(['endif']);\n    }\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/TokenParser/IfTokenParser.php#L49-L85","documentation":"Thrown by IfTokenParser::parse when template parsing reaches the end of the template (or an unexpected closing tag) without finding {% else %}, {% elseif %}, or {% endif %} to close an {% if %} opened at the given line. The default case of the subparse loop converts the token into this SyntaxError so the developer knows exactly where the unclosed if started.","triggerScenarios":"An {% if %} block whose {% endif %} is missing, deleted, or placed after an {% endblock %}/{% endmacro %} that ends the parse region; EOF hit while subparsing the if body.","commonSituations":"Large template edits that accidentally remove an endif; nesting ifs inside blocks/macros and closing the outer construct first; conditional blocks generated by partials where endif was expected in another fragment.","solutions":["Add the missing {% endif %} for the if started at the reported line.","Check that elseif/else branches are all properly closed before the enclosing endblock/endmacro.","Recount nesting after a refactor — an extra {% endif %} elsewhere may have consumed this one.","Use an editor with Twig syntax highlighting / lint to spot unbalanced tags."],"exampleFix":"{% if cond %}\n  <p>hi</p>\n\n{# after #}\n{% if cond %}\n  <p>hi</p>\n{% endif %}","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(), 'looking for the following tags')) {\n        // use the reported start line to locate the unclosed {% if %}\n    }\n    throw $e;\n}","preventionTips":["Close every {% if %} with {% endif %} in the same file/region.","Use a Twig-aware editor or twig-cs-fixer to detect unbalanced tags.","When nesting ifs inside blocks/macros, close inner constructs first."],"tags":["twig","if","syntax-error","unclosed-tag"],"backgroundTag":"unclosed-tag","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"}