twigphp/Twig · error · SyntaxError
When using set with a block, you cannot have a multi-target.
Error message
When using set with a block, you cannot have a multi-target.
What it means
The capture form of {% set %} ({% set name %}...{% endset %}) can only target a single variable. Multi-target capture is meaningless because the captured block produces one string, so Twig throws a syntax error.
Solutions
- Capture into one variable, then derive the second: '{% set all %}...{% endset %}{% set a, b = all|split(',') %}'
- Use two separate captured sets if the contents differ
- Use the '=' form with equal counts instead of capture for multi-target assignment
Example fix
// before
{% set a, b %}hello{% endset %}
// after
{% set a %}hello{% endset %}{% set b = a ~ '!' %} Defensive patterns
Strategy: validation
Validate before calling
// Multi-target capture sets are invalid
if (preg_match('/\{%\s*set\s+[^=}%]*,[^=}%]*\s*%}(?:.|\n)*?\{%\s*endset\s*%}/', $src)) {
throw new InvalidArgumentException('Capture form of set supports a single target.');
} Try / catch
try {
$twig->render($template, $ctx);
} catch (SyntaxError $e) {
if (str_contains($e->getMessage(), 'multi-target')) {
// rewrite template to single-target capture
}
throw $e;
} Prevention
- Use captured sets only with one variable
- Split multi-target logic into separate capture blocks
- Grep for '{% set' with commas followed by a block to catch this early
When it happens
Trigger: Writing '{% set a, b %}...{% endset %}' — in parse, when no '=' follows the names and count($names) > 1, the error is raised.
Common situations: Converting a plain multi-target set ('{% set a, b = 1, 2 %}') into a captured block; misunderstanding capture semantics after seeing multi-target assignment elsewhere.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- When using set, you must have the same number of variables…
- Unexpected character
- The variadic argument
- The variadic argument
- The template references in a "use" statement must be a…
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/fde3ac07a3dd3af1.
Report an issue: GitHub.
Appendix: source
Thrown at src/TokenParser/SetTokenParser.php:53
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$names = $this->parseAssignmentExpression();
$capture = false;
if ($stream->nextIf(Token::OPERATOR_TYPE, '=')) {
$values = $this->parseMultitargetExpression();
$stream->expect(Token::BLOCK_END_TYPE);
if (\count($names) !== \count($values)) {
throw new SyntaxError('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
} else {
$capture = true;
if (\count($names) > 1) {
throw new SyntaxError('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
$stream->expect(Token::BLOCK_END_TYPE);
$values = $this->parser->subparse([$this, 'decideBlockEnd'], true);
$stream->expect(Token::BLOCK_END_TYPE);
}
return new SetNode($capture, $names, $values, $lineno);
}
public function decideBlockEnd(Token $token): bool
{
return $token->test('endset');
}
public function getTag(): string
{View on GitHub (pinned to a414c3a491)