{"record":{"id":"613ed53fad680bdd","repo":"twigphp/Twig","slug":"block-s-should-not-call-parent-in-s-as-the-block-does-not","errorCode":null,"errorMessage":"Block \"%s\" should not call parent() in \"%s\" as the block does not exist in the parent template \"%s\".","messagePattern":"Block \"(.+?)\" should not call parent\\(\\) in \"(.+?)\" as the block does not exist in the parent template \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Template.php","lineNumber":483,"sourceCode":"            $block = null;\n        }\n\n        // avoid RCEs when sandbox is enabled\n        if (null !== $template && !$template instanceof self) {\n            throw new \\LogicException('A block must be a method on a \\Twig\\Template instance.');\n        }\n\n        if (null !== $template) {\n            try {\n                $template->ensureSecurityChecked();\n                yield from $template->$block($context, $blocks);\n            } catch (\\Throwable $e) {\n                $template->handleException($e);\n            }\n        } elseif ($parent = $this->getParent($context)) {\n            yield from $parent->unwrap()->yieldBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this);\n        } elseif (isset($blocks[$name])) {\n            throw new RuntimeError(\\sprintf('Block \"%s\" should not call parent() in \"%s\" as the block does not exist in the parent template \"%s\".', $name, $blocks[$name][0]->getTemplateName(), $this->getTemplateName()), -1, $blocks[$name][0]->getSourceContext());\n        } else {\n            throw new RuntimeError(\\sprintf('Block \"%s\" on template \"%s\" does not exist.', $name, $this->getTemplateName()), -1, ($templateContext ?? $this)->getSourceContext());\n        }\n    }\n\n    /**\n     * Yields a parent block.\n     *\n     * This method is for internal use only and should never be called\n     * directly.\n     *\n     * @param string $name    The block name to display from the parent\n     * @param array  $context The context\n     * @param array  $blocks  The current set of blocks\n     *\n     * @return iterable<scalar|\\Stringable|null>\n     */\n    public function yieldParentBlock($name, array $context, array $blocks = []): iterable","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Template.php#L465-L501","documentation":"Thrown by Template::yieldBlock when a template's parent() function is invoked (or a block is rendered with inheritance fallback) but the block being asked for is only defined in the current template's own block set, not in the parent template. Twig detects that parent() was called for a block that has no counterpart anywhere up the inheritance chain and refuses, naming both the block and the template. It is a RuntimeError because it happens at render time, not parse time.","triggerScenarios":"Calling {{ parent() }} inside a {% block %} that is not an override of a block defined in the parent template (e.g. the parent defines no block with that name). Reached when yieldBlock is called by displayBlock/renderBlock and the parent exists but $blocks[$name] points only at the current template.","commonSituations":"Renaming a block in the parent template but forgetting to rename it in the child, so the child block is brand new yet still calls parent(); copy-pasting a block override (with parent() inside) into a template whose parent lacks that block; adding a child template between two levels in an inheritance chain and dropping a block definition.","solutions":["Remove the parent() call from the block, or delete the block entirely if it adds nothing.","Add a block with the same name to the parent template so parent() has something to yield.","Check the template inheritance chain ({% extends %}) to confirm the parent really defines the block at every level.","Use {% use %} (traits) or {% include %} instead of inheritance if the block should not exist in the parent."],"exampleFix":"{% block sidebar %}\n  {{ parent() }} {# parent template has no 'sidebar' block #}\n{% endblock %}\n\n{# after: either remove parent() or define the block in the parent #}\n{% block sidebar %}\n  <nav>...</nav>\n{% endblock %}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $twig->render('child.twig', $ctx);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'should not call parent()')) {\n        // log block/parent mismatch and render fallback template\n    }\n    throw $e;\n}","preventionTips":["Only call parent() in blocks that override a same-named block in the parent chain.","Rename blocks in parent and child templates together in one commit.","Run template rendering in CI against representative data to catch inheritance breakage."],"tags":["twig","template-inheritance","block","runtime-error"],"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"}