Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Template.php

function Template::yieldBlock

Return value

iterable<scalar|\Stringable|null>

2 calls to Template::yieldBlock()
Template::displayBlock in vendor/twig/twig/src/Template.php
Displays a block.
Template::renderBlock in vendor/twig/twig/src/Template.php
Renders a block.

File

vendor/twig/twig/src/Template.php, line 411

Class

Template
Default base class for compiled templates.

Namespace

Twig

Code

public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null) : iterable {
    if ($useBlocks && isset($blocks[$name])) {
        $template = $blocks[$name][0];
        $block = $blocks[$name][1];
    }
    elseif (isset($this->blocks[$name])) {
        $template = $this->blocks[$name][0];
        $block = $this->blocks[$name][1];
    }
    else {
        $template = null;
        $block = null;
    }
    // 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 {
            yield from $template->{$block}($context, $blocks);
        } catch (Error $e) {
            if (!$e->getSourceContext()) {
                $e->setSourceContext($template->getSourceContext());
            }
            // this is mostly useful for \Twig\Error\LoaderError exceptions
            // see \Twig\Error\LoaderError
            if (-1 === $e->getTemplateLine()) {
                $e->guess();
            }
            throw $e;
        } catch (\Throwable $e) {
            $e = new RuntimeError(\sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getSourceContext(), $e);
            $e->guess();
            throw $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());
    }
}
RSS feed
Powered by Drupal