{"record":{"id":"ea262a7df8d136dc","repo":"twigphp/Twig","slug":"variable-s-does-not-exist","errorCode":null,"errorMessage":"Variable %s does not exist.","messagePattern":"Variable (.+?) does not exist\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/NameExpression.php","lineNumber":85,"sourceCode":"                ->string($name)\n                ->raw(']')\n            ;\n        } else {\n            if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {\n                $compiler\n                    ->raw('($context[')\n                    ->string($name)\n                    ->raw('] ?? null)')\n                ;\n            } else {\n                $compiler\n                    ->raw('(isset($context[')\n                    ->string($name)\n                    ->raw(']) || array_key_exists(')\n                    ->string($name)\n                    ->raw(', $context) ? $context[')\n                    ->string($name)\n                    ->raw('] : (function () { throw new RuntimeError(\\'Variable ')\n                    ->string($name)\n                    ->raw(' does not exist.\\', ')\n                    ->repr($this->lineno)\n                    ->raw(', $this->source); })()')\n                    ->raw(')')\n                ;\n            }\n        }\n    }\n\n    /**\n     * @deprecated since Twig 3.11 (to be removed in 4.0)\n     */\n    public function isSpecial()\n    {\n        trigger_deprecation('twig/twig', '3.11', 'The \"%s()\" method is deprecated and will be removed in Twig 4.0.', __METHOD__);\n\n        return isset($this->specialVars[$this->getAttribute('name')]);","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/NameExpression.php#L67-L103","documentation":"This RuntimeError is thrown at runtime when a template accesses an undefined variable. Twig compiles variable reads into a check that the name exists in $context; if not, the generated closure raises this error with the template line and source.","triggerScenarios":"Rendering {{ foo }} or {% if foo %} where 'foo' was never passed to $twig->render($template, $context) or set by for/set/import.","commonSituations":"Typos in variable names; forgetting a key in the render context array; relying on PHP notice-suppression semantics that Twig does not replicate; renaming variables after refactoring.","solutions":["Add the missing key to the context array passed to render()","Fix the variable name typo in the template","Provide a default: {{ foo|default('') }} or {% if foo is defined %}","Enable Twig strict_variables explicitly during development to catch these early"],"exampleFix":"// before\nreturn $twig->render('user.html', ['name' => $name]); // template uses {{ email }}\n// after\nreturn $twig->render('user.html', ['name' => $name, 'email' => $email]);","handlingStrategy":"try-catch","validationCode":"// Verify required context keys before rendering\n$required = ['user', 'email'];\n$missing = array_diff($required, array_keys($context));\nif ($missing) {\n    throw new InvalidArgumentException('Missing context keys: '.implode(',', $missing));\n}","typeGuard":"// Template-level guard\n// {% if email is defined %}{{ email }}{% endif %}","tryCatchPattern":"try {\n    return $twig->render($template, $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), 'does not exist')) {\n        // log template + line, fix context\n    }\n    throw $e;\n}","preventionTips":["Use strict_variables: true in dev to fail fast","Keep a documented context schema per template","Prefer {{ var|default(...) }} for optional values","Grep templates for variable names when refactoring contexts"],"tags":["twig","runtime","undefined-variable","template"],"backgroundTag":"undefined-variable","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"}