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

Breadcrumb

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

function MacroNode::compile

Overrides Node::compile

File

vendor/twig/twig/src/Node/MacroNode.php, line 59

Class

MacroNode
Represents a macro node.

Namespace

Twig\Node

Code

public function compile(Compiler $compiler) : void {
    $compiler->addDebugInfo($this)
        ->write(\sprintf('public function macro_%s(', $this->getAttribute('name')));
    
    /** @var ArrayExpression $arguments */
    $arguments = $this->getNode('arguments');
    foreach ($arguments->getKeyValuePairs() as $pair) {
        $name = $pair['key'];
        $default = $pair['value'];
        $compiler->subcompile($name)
            ->raw(' = ')
            ->subcompile($default)
            ->raw(', ');
    }
    $compiler->raw('...$varargs')
        ->raw("): string|Markup\n")
        ->write("{\n")
        ->indent()
        ->write("\$macros = \$this->macros;\n")
        ->write("\$context = [\n")
        ->indent();
    foreach ($arguments->getKeyValuePairs() as $pair) {
        $name = $pair['key'];
        $var = $name->getAttribute('name');
        if (str_starts_with($var, "͜")) {
            $var = substr($var, \strlen("͜"));
        }
        $compiler->write('')
            ->string($var)
            ->raw(' => ')
            ->subcompile($name)
            ->raw(",\n");
    }
    $node = new CaptureNode($this->getNode('body'), $this->getNode('body')->lineno);
    $compiler->write('')
        ->string(self::VARARGS_NAME)
        ->raw(' => ')
        ->raw("\$varargs,\n")
        ->outdent()
        ->write("] + \$this->env->getGlobals();\n\n")
        ->write("\$blocks = [];\n\n")
        ->write('return ')
        ->subcompile($node)
        ->raw("\n")
        ->outdent()
        ->write("}\n\n");
}
RSS feed
Powered by Drupal