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

Breadcrumb

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

class WithNode

Represents a nested "with" scope.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
    • class \Twig\Node\WithNode extends \Twig\Node\Node

Expanded class hierarchy of WithNode

1 file declares its use of WithNode
WithTokenParser.php in vendor/twig/twig/src/TokenParser/WithTokenParser.php

File

vendor/twig/twig/src/Node/WithNode.php, line 22

Namespace

Twig\Node
View source
class WithNode extends Node {
    public function __construct(Node $body, ?Node $variables, bool $only, int $lineno) {
        $nodes = [
            'body' => $body,
        ];
        if (null !== $variables) {
            $nodes['variables'] = $variables;
        }
        parent::__construct($nodes, [
            'only' => $only,
        ], $lineno);
    }
    public function compile(Compiler $compiler) : void {
        $compiler->addDebugInfo($this);
        $parentContextName = $compiler->getVarName();
        $compiler->write(\sprintf("\$%s = \$context;\n", $parentContextName));
        if ($this->hasNode('variables')) {
            $node = $this->getNode('variables');
            $varsName = $compiler->getVarName();
            $compiler->write(\sprintf('$%s = ', $varsName))
                ->subcompile($node)
                ->raw(";\n")
                ->write(\sprintf("if (!is_iterable(\$%s)) {\n", $varsName))
                ->indent()
                ->write("throw new RuntimeError('Variables passed to the \"with\" tag must be a mapping.', ")
                ->repr($node->getTemplateLine())
                ->raw(", \$this->getSourceContext());\n")
                ->outdent()
                ->write("}\n")
                ->write(\sprintf("\$%s = CoreExtension::toArray(\$%s);\n", $varsName, $varsName));
            if ($this->getAttribute('only')) {
                $compiler->write("\$context = [];\n");
            }
            $compiler->write(\sprintf("\$context = \$%s + \$context + \$this->env->getGlobals();\n", $varsName));
        }
        $compiler->subcompile($this->getNode('body'))
            ->write(\sprintf("\$context = \$%s;\n", $parentContextName));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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
WithNode::compile public function Overrides Node::compile
WithNode::__construct public function Overrides Node::__construct
RSS feed
Powered by Drupal