{"record":{"id":"9be5f5cacad9fb59","repo":"twigphp/Twig","slug":"block-s-on-template-s-does-not-exist","errorCode":null,"errorMessage":"Block \"%s\" on template \"%s\" does not exist.","messagePattern":"Block \"(.+?)\" on template \"(.+?)\" does not exist\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"src/BlockChain.php","lineNumber":189,"sourceCode":"                $lineage[] = $template;\n\n                $parent = $template->getParent($context);\n                $fixed = $fixed && $template->hasFixedParent();\n\n                // a dynamic parent expression can evaluate to a template from another environment\n                $template = $parent instanceof TemplateWrapper ? $parent->unwrap() : $parent;\n                if (false !== $template && !$template->isOwnedBy($this->env)) {\n                    throw new \\LogicException('A block chain cannot contain templates from different Twig environments.');\n                }\n            } while (false !== $template);\n        }\n\n        return [$lineage, $fixed];\n    }\n\n    private function throwUnknownBlock(string $name): never\n    {\n        throw new RuntimeError(\\sprintf('Block \"%s\" on template \"%s\" does not exist.', $name, $this->templates[0]->getTemplateName()), -1, $this->templates[0]->getSourceContext());\n    }\n}\n","sourceCodeStart":171,"sourceCodeEnd":192,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/BlockChain.php#L171-L192","documentation":"BlockChain tracks the chain of template inheritance for a rendered template. throwUnknownBlock raises a RuntimeError when a block name cannot be found anywhere in the template's lineage. It is thrown by streamBlock, renderBlock and displayBlock, meaning a caller (template or PHP code) asked for a block that no template in the inheritance chain defines.","triggerScenarios":"Calling $template->renderBlock('name', ...), displayBlock(), or streamBlock() for a block that does not exist in the template or any of its parents; a compiled child template referencing a parent block via $this->getParent() chains that has been renamed or removed.","commonSituations":"Renaming or deleting a {% block %} in a parent template while children still override or render it; calling renderBlock() from PHP with a misspelled block name; template inheritance misconfiguration (extends pointing at the wrong parent); stale compiled templates after refactoring.","solutions":["Verify the block name exists (grep the template and its parents for {% block name %}).","Fix typos or update call sites after renaming a block.","Clear the Twig cache (rm -rf var/cache/twig or $twig->getCache()->clear equivalent) so compiled templates match current sources.","Use $template->hasBlock('name', $context) before renderBlock/displayBlock to check existence."],"exampleFix":"// before\nif ($template->hasBlock('content', $context)) {\n    echo $template->renderBlock('content', $context);\n}\necho $template->renderBlock('contnet', $context); // typo -> RuntimeError\n// after\nif ($template->hasBlock('content', $context)) {\n    echo $template->renderBlock('content', $context);\n}","handlingStrategy":"type-guard","validationCode":"<?php\nif (!$template->hasBlock('content', $context)) {\n    throw new InvalidArgumentException(\"Template '{$template->getTemplateName()}' has no block 'content'\");\n}\necho $template->renderBlock('content', $context);","typeGuard":"<?php\nfunction canRenderBlock(Twig\\Template $template, string $name, array $context = []): bool\n{\n    return $template->hasBlock($name, $context);\n}","tryCatchPattern":"<?php\nuse Twig\\Error\\RuntimeError;\ntry {\n    $html = $template->renderBlock('content', $context);\n} catch (RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'does not exist')) {\n        $html = ''; // or a default block\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Always call hasBlock() before renderBlock()/displayBlock() when the block name is dynamic.","Keep parent/child template blocks in sync; grep for block usages before renaming or deleting a block.","Clear the Twig cache after template refactors to purge stale compiled templates.","Add integration tests that render each template with its inheritance chain."],"tags":["php","twig","template","block"],"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"}