twigphp/Twig · error · RuntimeError

The template has no parent and no traits defining the

Error message

The template has no parent and no traits defining the "%s" block.

What it means

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.

Solutions

  1. Remove the parent() call from the block.
  2. Add {% extends 'parent.twig' %} if this template was supposed to override a parent.
  3. Use {% use 'blocks.twig' %} to import a trait defining the block so parent() resolves to it.
  4. Inline the shared markup directly or via {% include %} instead of relying on parent().

Example fix

{# standalone.twig (no extends) #}
{% block content %}{{ parent() }}{% endblock %}

{# after #}
{% extends 'base.twig' %}
{% block content %}{{ parent() }}{% endblock %}
Defensive patterns

Strategy: type-guard

Try / catch

try {
    $html = $twig->render($tpl, $ctx);
} catch (\Twig\Error\RuntimeError $e) {
    if (str_contains($e->getMessage(), 'no parent and no traits')) {
        $html = $twig->render($fallbackTpl, $ctx);
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/3e2b1410154eb789. Report an issue: GitHub.

Appendix: source

Thrown at src/Template.php:508

     * Yields a parent block.
     *
     * This method is for internal use only and should never be called
     * directly.
     *
     * @param string $name    The block name to display from the parent
     * @param array  $context The context
     * @param array  $blocks  The current set of blocks
     *
     * @return iterable<scalar|\Stringable|null>
     */
    public function yieldParentBlock($name, array $context, array $blocks = []): iterable
    {
        if (isset($this->traits[$name])) {
            yield from $this->traits[$name][0]->yieldBlock($this->traitAliases[$name] ?? $name, $context, $blocks, false);
        } elseif ($parent = $this->getParent($context)) {
            yield from $parent->unwrap()->yieldBlock($name, $context, $blocks, false);
        } else {
            throw new RuntimeError(\sprintf('The template has no parent and no traits defining the "%s" block.', $name), -1, $this->getSourceContext());
        }
    }

    /**
     * @internal
     */
    public function getMacroNamespace(): MacroNamespace
    {
        return $this->macroNamespace ??= new MacroNamespace($this, $this->loadDeclaredMacros());
    }

    /**
     * @return array<string, TwigMacro>
     */
    protected function loadDeclaredMacros(): array
    {
        return [];
    }

View on GitHub (pinned to a414c3a491)