class MacroNode
Represents a macro node.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
- class \Twig\Node\MacroNode extends \Twig\Node\Node
Expanded class hierarchy of MacroNode
3 files declare their use of MacroNode
- MacroTokenParser.php in vendor/
twig/ twig/ src/ TokenParser/ MacroTokenParser.php - Parser.php in vendor/
twig/ twig/ src/ Parser.php - ProfilerNodeVisitor.php in vendor/
twig/ twig/ src/ Profiler/ NodeVisitor/ ProfilerNodeVisitor.php
File
-
vendor/
twig/ twig/ src/ Node/ MacroNode.php, line 25
Namespace
Twig\NodeView source
class MacroNode extends Node {
public const VARARGS_NAME = 'varargs';
/**
* @param BodyNode $body
* @param ArrayExpression $arguments
*/
public function __construct(string $name, Node $body, Node $arguments, int $lineno) {
if (!$body instanceof BodyNode) {
trigger_deprecation('twig/twig', '3.12', \sprintf('Not passing a "%s" instance as the "body" argument of the "%s" constructor is deprecated ("%s" given).', BodyNode::class, static::class, $body::class));
}
if (!$arguments instanceof ArrayExpression) {
trigger_deprecation('twig/twig', '3.15', \sprintf('Not passing a "%s" instance as the "arguments" argument of the "%s" constructor is deprecated ("%s" given).', ArrayExpression::class, static::class, $arguments::class));
$args = new ArrayExpression([], $arguments->getTemplateLine());
foreach ($arguments as $name => $default) {
$args->addElement($default, new LocalVariable($name, $default->getTemplateLine()));
}
$arguments = $args;
}
foreach ($arguments->getKeyValuePairs() as $pair) {
if ("͜" . self::VARARGS_NAME === $pair['key']->getAttribute('name')) {
throw new SyntaxError(\sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $pair['value']->getTemplateLine(), $pair['value']->getSourceContext());
}
}
parent::__construct([
'body' => $body,
'arguments' => $arguments,
], [
'name' => $name,
], $lineno);
}
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");
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
MacroNode::compile | public | function | Overrides Node::compile | ||
MacroNode::VARARGS_NAME | public | constant | |||
MacroNode::__construct | public | function | Overrides Node::__construct | ||
Node::$attributeNameDeprecations | private | property | @var array<string, NameDeprecation> | ||
Node::$attributes | protected | property | |||
Node::$lineno | protected | property | |||
Node::$nodeNameDeprecations | private | property | @var array<string, NameDeprecation> | ||
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 |