{"record":{"id":"a4c6efb081d8ad42","repo":"twigphp/Twig","slug":"you-cannot-assign-a-value-to-s","errorCode":null,"errorMessage":"You cannot assign a value to \"%s\".","messagePattern":"You cannot assign a value to \"(.+?)\"\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/AssignNameExpression.php","lineNumber":30,"sourceCode":"\nnamespace Twig\\Node\\Expression;\n\nuse Twig\\Compiler;\nuse Twig\\Error\\SyntaxError;\nuse Twig\\Node\\Expression\\Variable\\AssignContextVariable;\nuse Twig\\Node\\Expression\\Variable\\ContextVariable;\n\nclass AssignNameExpression extends ContextVariable\n{\n    public function __construct(string $name, int $lineno)\n    {\n        if (self::class === static::class) {\n            trigger_deprecation('twig/twig', '3.15', 'The \"%s\" class is deprecated, use \"%s\" instead.', self::class, AssignContextVariable::class);\n        }\n\n        // All names supported by ExpressionParser::parsePrimaryExpression() should be excluded\n        if (\\in_array(strtolower($name), ['true', 'false', 'none', 'null'], true)) {\n            throw new SyntaxError(\\sprintf('You cannot assign a value to \"%s\".', $name), $lineno);\n        }\n\n        parent::__construct($name, $lineno);\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        $compiler\n            ->raw('$context[')\n            ->string($this->getAttribute('name'))\n            ->raw(']')\n        ;\n    }\n}\n","sourceCodeStart":12,"sourceCodeEnd":45,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/AssignNameExpression.php#L12-L45","documentation":"Twig raises this SyntaxError at compile time when a template tries to assign to a reserved literal name: true, false, none, or null (case-insensitive). These tokens are constants/literals in Twig, not variables, so they can never be assignment targets. AssignNameExpression's constructor rejects them before the template is compiled.","triggerScenarios":"Instantiating AssignNameExpression (or compiling a template containing) `{% set true = ... %}`, `{% set FALSE = ... %}`, `{% set null = x %}`, `{% for none in items %}`, or macro/import blocks targeting those names; also direct PHP construction `new AssignNameExpression('true', $lineno)`.","commonSituations":"Typos like `{% set false = someVar %}` when meaning to test a value; developers porting from languages where `true`/`null` are valid identifiers; generated templates that build `set` targets from unvalidated user input.","solutions":["Rename the assignment target in the template to a valid variable name (e.g. `isTrue`, `isNull`).","If the value should be a literal, drop the assignment and use `true`/`false`/`none`/`null` directly in expressions.","In PHP code building nodes, validate the name against ['true','false','none','null'] before constructing AssignNameExpression."],"exampleFix":"// before (template)\n{% set true = user.isActive %}\n// after\n{% set isActive = user.isActive %}","handlingStrategy":"validation","validationCode":"$reserved = ['true', 'false', 'none', 'null'];\nif (in_array(strtolower($name), $reserved, true)) {\n    throw new \\InvalidArgumentException(\"'$name' is a Twig reserved word and cannot be a variable name\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use Twig literals (true/false/none/null) as set/for/macro target names.","Validate dynamically generated variable names against reserved words before rendering.","Prefix boolean variable names (isX, hasX) instead of naming them after literals."],"tags":["twig","syntax-error","template-compilation","reserved-keyword"],"backgroundTag":"invalid-identifier","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"}