{"record":{"id":"3e2b1410154eb789","repo":"twigphp/Twig","slug":"the-template-has-no-parent-and-no-traits-defining-the-s","errorCode":null,"errorMessage":"The template has no parent and no traits defining the \"%s\" block.","messagePattern":"The template has no parent and no traits defining the \"(.+?)\" block\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Template.php","lineNumber":508,"sourceCode":"     * 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\n    {\n        if (isset($this->traits[$name])) {\n            yield from $this->traits[$name][0]->yieldBlock($this->traitAliases[$name] ?? $name, $context, $blocks, false);\n        } elseif ($parent = $this->getParent($context)) {\n            yield from $parent->unwrap()->yieldBlock($name, $context, $blocks, false);\n        } else {\n            throw new RuntimeError(\\sprintf('The template has no parent and no traits defining the \"%s\" block.', $name), -1, $this->getSourceContext());\n        }\n    }\n\n    /**\n     * @internal\n     */\n    public function getMacroNamespace(): MacroNamespace\n    {\n        return $this->macroNamespace ??= new MacroNamespace($this, $this->loadDeclaredMacros());\n    }\n\n    /**\n     * @return array<string, TwigMacro>\n     */\n    protected function loadDeclaredMacros(): array\n    {\n        return [];\n    }","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Template.php#L490-L526","documentation":"Thrown by Template::yieldParentBlock when parent() is evaluated in a context where the template neither extends a parent nor has a {% use %} trait providing the block. With nothing to delegate to, Twig raises a RuntimeError. parent() is only meaningful inside an overriding block of a template that has a parent or trait.","triggerScenarios":"Calling {{ parent() }} inside a block in a template that has no {% extends %} and no {% use %} supplying that block; also reached via displayParentBlock/renderParentBlock on a standalone template.","commonSituations":"Copying a block override (with parent() inside) into a standalone template; removing an {% extends %} line during refactoring but keeping the block body that calls parent(); using parent() inside an {% embed %} template's block where the embed target has no such block.","solutions":["Remove the parent() call from the block.","Add {% extends 'parent.twig' %} if this template was supposed to override a parent.","Use {% use 'blocks.twig' %} to import a trait defining the block so parent() resolves to it.","Inline the shared markup directly or via {% include %} instead of relying on parent()."],"exampleFix":"{# standalone.twig (no extends) #}\n{% block content %}{{ parent() }}{% endblock %}\n\n{# after #}\n{% extends 'base.twig' %}\n{% block content %}{{ parent() }}{% endblock %}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $html = $twig->render($tpl, $ctx);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'no parent and no traits')) {\n        $html = $twig->render($fallbackTpl, $ctx);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Never use parent() in templates without {% extends %} or {% use %}.","Audit standalone templates for parent() calls after removing extends lines.","Centralize shared block markup in an included partial instead of relying on inheritance."],"tags":["twig","template-inheritance","parent","runtime-error"],"backgroundTag":"invalid-state-transition","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"}