{"record":{"id":"fde3ac07a3dd3af1","repo":"twigphp/Twig","slug":"when-using-set-with-a-block-you-cannot-have-a-multi-target","errorCode":null,"errorMessage":"When using set with a block, you cannot have a multi-target.","messagePattern":"When using set with a block, you cannot have a multi-target\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/TokenParser/SetTokenParser.php","lineNumber":53,"sourceCode":"    {\n        $lineno = $token->getLine();\n        $stream = $this->parser->getStream();\n        $names = $this->parseAssignmentExpression();\n\n        $capture = false;\n        if ($stream->nextIf(Token::OPERATOR_TYPE, '=')) {\n            $values = $this->parseMultitargetExpression();\n\n            $stream->expect(Token::BLOCK_END_TYPE);\n\n            if (\\count($names) !== \\count($values)) {\n                throw new SyntaxError('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext());\n            }\n        } else {\n            $capture = true;\n\n            if (\\count($names) > 1) {\n                throw new SyntaxError('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext());\n            }\n\n            $stream->expect(Token::BLOCK_END_TYPE);\n\n            $values = $this->parser->subparse([$this, 'decideBlockEnd'], true);\n            $stream->expect(Token::BLOCK_END_TYPE);\n        }\n\n        return new SetNode($capture, $names, $values, $lineno);\n    }\n\n    public function decideBlockEnd(Token $token): bool\n    {\n        return $token->test('endset');\n    }\n\n    public function getTag(): string\n    {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/TokenParser/SetTokenParser.php#L35-L71","documentation":"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.","triggerScenarios":"Writing '{% set a, b %}...{% endset %}' — in parse, when no '=' follows the names and count($names) > 1, the error is raised.","commonSituations":"Converting a plain multi-target set ('{% set a, b = 1, 2 %}') into a captured block; misunderstanding capture semantics after seeing multi-target assignment elsewhere.","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"],"exampleFix":"// before\n{% set a, b %}hello{% endset %}\n// after\n{% set a %}hello{% endset %}{% set b = a ~ '!' %}","handlingStrategy":"validation","validationCode":"// Multi-target capture sets are invalid\nif (preg_match('/\\{%\\s*set\\s+[^=}%]*,[^=}%]*\\s*%}(?:.|\\n)*?\\{%\\s*endset\\s*%}/', $src)) {\n    throw new InvalidArgumentException('Capture form of set supports a single target.');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $twig->render($template, $ctx);\n} catch (SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'multi-target')) {\n        // rewrite template to single-target capture\n    }\n    throw $e;\n}","preventionTips":["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"],"tags":["twig","set","syntax-error","template"],"backgroundTag":"invalid-argument-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}