twigphp/Twig · error · RuntimeError
Block " " on template " " does not exist.
Error message
Block "%s" on template "%s" does not exist.
What it means
Thrown by Template::yieldBlock as the final fallback: the requested block name exists neither in the template's own blocks, nor in any merged block set from callers, nor in a parent template. Twig raises a RuntimeError naming the missing block and template. This is the generic 'block not found' failure at render time.
Solutions
- Fix the block name typo in the block() call or {% block %} tag.
- Define the missing block in the template or in a parent template.
- If the block lives in another template, use {% extends %}, {% use %}, or the two-argument block('name', 'other.twig') form.
- Verify the template actually being rendered is the one you expect (check {% extends %} and include paths).
Example fix
{{ block('contnt') }}
{# after #}
{{ block('content') }} Defensive patterns
Strategy: try-catch
Validate before calling
$blocks = $twig->load('base.twig')->getSourceContext(); // or grep template source for '{% block name %}' before calling block('name') Try / catch
try {
return $twig->render('page.twig', $ctx);
} catch (\Twig\Error\RuntimeError $e) {
if (str_contains($e->getMessage(), 'does not exist')) {
return $fallbackTemplate;
}
throw $e;
} Prevention
- Grep templates for block() references and confirm each name is defined somewhere in the chain.
- Avoid typos by reusing the same block-name constants in code and templates.
- Keep block definitions in the direct parent or a {% use %} trait so resolution is local.
When it happens
Trigger: Calling block('name') in a template/expression, block('name', template) via TemplateWrapper or Template::renderBlock, or yieldBlock() directly, where no template in the inheritance/trait chain defines 'name'.
Common situations: Typo in the block name inside a block() expression or function call; rendering a template directly that was meant to be included/extended; refactoring that renamed a block but not all references; calling block() before the defining template is loaded via {% extends %}/{% use %}.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Block " " on template " " does not exist.
- Block " " should not call parent() in " " as the block does…
- The "html_classes" function argument
- Failed to load Twig template
- The "cycle" function expects a non-empty sequence.
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/7cf8922128e7ca03.
Report an issue: GitHub.
Appendix: source
Thrown at src/Template.php:485
// avoid RCEs when sandbox is enabled
if (null !== $template && !$template instanceof self) {
throw new \LogicException('A block must be a method on a \Twig\Template instance.');
}
if (null !== $template) {
try {
$template->ensureSecurityChecked();
yield from $template->$block($context, $blocks);
} catch (\Throwable $e) {
$template->handleException($e);
}
} elseif ($parent = $this->getParent($context)) {
yield from $parent->unwrap()->yieldBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this);
} elseif (isset($blocks[$name])) {
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());
} else {
throw new RuntimeError(\sprintf('Block "%s" on template "%s" does not exist.', $name, $this->getTemplateName()), -1, ($templateContext ?? $this)->getSourceContext());
}
}
/**
* 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])) {View on GitHub (pinned to a414c3a491)