twigphp/Twig · error · SyntaxError
You cannot assign a value to
Error message
You cannot assign a value to "%s".
What it means
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.
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.
Example fix
// before (template)
{% set true = user.isActive %}
// after
{% set isActive = user.isActive %} Defensive patterns
Strategy: validation
Validate before calling
$reserved = ['true', 'false', 'none', 'null'];
if (in_array(strtolower($name), $reserved, true)) {
throw new \InvalidArgumentException("'$name' is a Twig reserved word and cannot be a variable name");
} Prevention
- 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.
When it happens
Trigger: 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)`.
Common situations: 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.
Related errors
- Cannot assign to " ", only variables can be assigned in…
- Cannot assign to " ", only variables can be assigned in…
- Positional arguments cannot be used after named arguments…
- Argument " " is defined twice for " ".
- An exception has been thrown during the compilation of a…
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/a4c6efb081d8ad42.
Report an issue: GitHub.
Appendix: source
Thrown at src/Node/Expression/AssignNameExpression.php:30
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
class AssignNameExpression extends ContextVariable
{
public function __construct(string $name, int $lineno)
{
if (self::class === static::class) {
trigger_deprecation('twig/twig', '3.15', 'The "%s" class is deprecated, use "%s" instead.', self::class, AssignContextVariable::class);
}
// All names supported by ExpressionParser::parsePrimaryExpression() should be excluded
if (\in_array(strtolower($name), ['true', 'false', 'none', 'null'], true)) {
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $name), $lineno);
}
parent::__construct($name, $lineno);
}
public function compile(Compiler $compiler): void
{
$compiler
->raw('$context[')
->string($this->getAttribute('name'))
->raw(']')
;
}
}
View on GitHub (pinned to a414c3a491)