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

Breadcrumb

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

class BlockReferenceExpression

Represents a block call node.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
    • class \Twig\Node\Expression\AbstractExpression extends \Twig\Node\Node
      • class \Twig\Node\Expression\BlockReferenceExpression extends \Twig\Node\Expression\AbstractExpression

Expanded class hierarchy of BlockReferenceExpression

4 files declare their use of BlockReferenceExpression
CoreExtension.php in vendor/twig/twig/src/Extension/CoreExtension.php
DefinedTest.php in vendor/twig/twig/src/Node/Expression/Test/DefinedTest.php
OptimizerNodeVisitor.php in vendor/twig/twig/src/NodeVisitor/OptimizerNodeVisitor.php
SafeAnalysisNodeVisitor.php in vendor/twig/twig/src/NodeVisitor/SafeAnalysisNodeVisitor.php

File

vendor/twig/twig/src/Node/Expression/BlockReferenceExpression.php, line 23

Namespace

Twig\Node\Expression
View source
class BlockReferenceExpression extends AbstractExpression {
    
    /**
     * @param AbstractExpression $name
     */
    public function __construct(Node $name, ?Node $template, int $lineno) {
        if (!$name instanceof AbstractExpression) {
            trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, get_class($name));
        }
        $nodes = [
            'name' => $name,
        ];
        if (null !== $template) {
            $nodes['template'] = $template;
        }
        parent::__construct($nodes, [
            'is_defined_test' => false,
            'output' => false,
        ], $lineno);
    }
    public function compile(Compiler $compiler) : void {
        if ($this->getAttribute('is_defined_test')) {
            $this->compileTemplateCall($compiler, 'hasBlock');
        }
        else {
            if ($this->getAttribute('output')) {
                $compiler->addDebugInfo($this);
                $compiler->write('yield from ');
                $this->compileTemplateCall($compiler, 'yieldBlock')
                    ->raw(";\n");
            }
            else {
                $this->compileTemplateCall($compiler, 'renderBlock');
            }
        }
    }
    private function compileTemplateCall(Compiler $compiler, string $method) : Compiler {
        if (!$this->hasNode('template')) {
            $compiler->write('$this');
        }
        else {
            $compiler->write('$this->loadTemplate(')
                ->subcompile($this->getNode('template'))
                ->raw(', ')
                ->repr($this->getTemplateName())
                ->raw(', ')
                ->repr($this->getTemplateLine())
                ->raw(')');
        }
        $compiler->raw(\sprintf('->unwrap()->%s', $method));
        return $this->compileBlockArguments($compiler);
    }
    private function compileBlockArguments(Compiler $compiler) : Compiler {
        $compiler->raw('(')
            ->subcompile($this->getNode('name'))
            ->raw(', $context');
        if (!$this->hasNode('template')) {
            $compiler->raw(', $blocks');
        }
        return $compiler->raw(')');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractExpression::hasExplicitParentheses public function
AbstractExpression::isGenerator public function
AbstractExpression::setExplicitParentheses public function
BlockReferenceExpression::compile public function Overrides Node::compile
BlockReferenceExpression::compileBlockArguments private function
BlockReferenceExpression::compileTemplateCall private function
BlockReferenceExpression::__construct public function Overrides Node::__construct
Node::$attributeNameDeprecations private property @var array&lt;string, NameDeprecation&gt;
Node::$attributes protected property
Node::$lineno protected property
Node::$nodeNameDeprecations private property @var array&lt;string, NameDeprecation&gt;
Node::$nodes protected property
Node::$sourceContext private property
Node::$tag protected property
Node::count public function
Node::deprecateAttribute public function
Node::deprecateNode public function
Node::getAttribute public function
Node::getIterator public function
Node::getNode public function
Node::getNodeTag public function
Node::getSourceContext public function
Node::getTemplateLine public function
Node::getTemplateName public function
Node::hasAttribute public function
Node::hasNode public function
Node::removeAttribute public function
Node::removeNode public function
Node::setAttribute public function
Node::setNode public function 1
Node::setNodeTag public function @internal
Node::setSourceContext public function
Node::__toString public function
RSS feed
Powered by Drupal