twigphp/Twig · error · InvalidArgumentException
Using " " for the value of node " " of " " is not…
Error message
Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.
What it means
The base Node constructor validates that every child in the $nodes map is itself a Node instance; passing anything else (string, array, scalar) is rejected with an InvalidArgumentException. Twig's AST is strictly tree-structured, and the compiler relies on every child being a Node.
Solutions
- Wrap raw values in the appropriate Node subclass (TextNode, ConstantExpression, etc.) before passing them in $nodes.
- Move non-node data (strings, flags, config) into the $attributes constructor argument instead of $nodes.
- Check get_debug_type($node) at the failing key to locate the offending entry.
Example fix
// before
new \Twig\Node\Node(['msg' => 'hello']);
// after
new \Twig\Node\Node(['msg' => new \Twig\Node\TextNode('hello')]); Defensive patterns
Strategy: type-guard
Validate before calling
foreach ($nodes as $name => $n) {
if (!$n instanceof \Twig\Node\Node) throw new InvalidArgumentException("Child '$name' must be a Node");
} Type guard
function isNodeMap(array $nodes): bool { foreach ($nodes as $n) { if (!$n instanceof \Twig\Node\Node) return false; } return true; } Try / catch
try {
$node = new MyNode($children, $attributes);
} catch (\InvalidArgumentException $e) {
// a child was not a Node; wrap raw values before constructing
} Prevention
- Remember $nodes takes Node children; scalars go in $attributes
- Wrap raw values in TextNode/ConstantExpression before passing
- Avoid instantiating Node directly (deprecated since Twig 3.15, abstract in 4.0)
When it happens
Trigger: Constructing any Node subclass (or Node itself, which is deprecated since 3.15) with a children array containing non-Node values, e.g. `new MyNode(['name' => 'text'])` instead of `new TextNode('text')`.
Common situations: Hand-writing custom Twig nodes/extensions; passing attributes (which belong in the $attributes argument) into the $nodes argument by mistake; instantiating Node directly after the 3.15 deprecation.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- EmptyNode cannot have children.
- Left side must be ArrayExpression for object/mapping…
- The "name" attribute must be a string.
- Using " " for the macro " " of " " is not supported. You…
- A " " can only contain " " nodes; replacing the macro " "…
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/a77be2fb243b188d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Node/Node.php:57
/** @var array<string, NameDeprecation> */
private $nodeNameDeprecations = [];
/** @var array<string, NameDeprecation> */
private $attributeNameDeprecations = [];
/**
* @param array<string|int, Node> $nodes An array of named nodes
* @param array $attributes An array of attributes (should not be nodes)
* @param int $lineno The line number
*/
public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0)
{
if (self::class === static::class) {
trigger_deprecation('twig/twig', '3.15', \sprintf('Instantiating "%s" directly is deprecated; the class will become abstract in 4.0.', self::class));
}
foreach ($nodes as $name => $node) {
if (!$node instanceof self) {
throw new \InvalidArgumentException(\sprintf('Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.', get_debug_type($node), $name, static::class));
}
}
$this->nodes = $nodes;
$this->attributes = $attributes;
$this->lineno = $lineno;
if (\func_num_args() > 3) {
trigger_deprecation('twig/twig', '3.12', \sprintf('The "tag" constructor argument of the "%s" class is deprecated and ignored (check which TokenParser class set it to "%s"), the tag is now automatically set by the Parser when needed.', static::class, func_get_arg(3) ?: 'null'));
}
}
public function __toString(): string
{
$repr = static::class;
if ($this->tag) {
$repr .= \sprintf("\n tag: %s", $this->tag);
}View on GitHub (pinned to a414c3a491)