{"record":{"id":"07bdeb23e1a0bd1a","repo":"twigphp/Twig","slug":"impossible-to-access-an-attribute-key-on-a-variable-dynamic","errorCode":null,"errorMessage":"Impossible to access an attribute/key on a variable (dynamic message).","messagePattern":"Impossible to access an attribute/key on a variable \\(dynamic message\\)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1823,"sourceCode":"                } elseif (\\is_array($object)) {\n                    if (!$object) {\n                        $message = \\sprintf('Key \"%s\" does not exist as the sequence/mapping is empty.', $arrayItem);\n                    } else {\n                        $message = \\sprintf('Key \"%s\" for sequence/mapping with keys \"%s\" does not exist.', $arrayItem, implode(', ', array_keys($object)));\n                    }\n                } elseif (Template::ARRAY_CALL === $type) {\n                    if (null === $object) {\n                        $message = \\sprintf('Impossible to access a key (\"%s\") on a null variable.', $item);\n                    } else {\n                        $message = \\sprintf('Impossible to access a key (\"%s\") on a %s variable (\"%s\").', $item, get_debug_type($object), $object);\n                    }\n                } elseif (null === $object) {\n                    $message = \\sprintf('Impossible to access an attribute (\"%s\") on a null variable.', $item);\n                } else {\n                    $message = \\sprintf('Impossible to access an attribute (\"%s\") on a %s variable (\"%s\").', $item, get_debug_type($object), $object);\n                }\n\n                throw new RuntimeError($message, $lineno, $source);\n            }\n        }\n\n        $item = (string) $item;\n\n        if (!\\is_object($object)) {\n            if ($isDefinedTest) {\n                return false;\n            }\n\n            if ($ignoreStrictCheck || !$env->isStrictVariables()) {\n                return;\n            }\n\n            if (null === $object) {\n                $message = \\sprintf('Impossible to invoke a method (\"%s\") on a null variable.', $item);\n            } elseif (\\is_array($object)) {\n                $message = \\sprintf('Impossible to invoke a method (\"%s\") on a sequence/mapping.', $item);","sourceCodeStart":1805,"sourceCodeEnd":1841,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1805-L1841","documentation":"CoreExtension::getAttribute() is the runtime backing for all attribute/key/method access in Twig templates (the `.` and `[]` operators). When the target variable is neither an array, ArrayAccess object, nor object (e.g. null, scalar, string), and Twig is running in strict_variables mode, it throws this RuntimeError with a message describing the variable's actual type. The generic \"dynamic message\" placeholder in this variant covers the fallthrough branches (null/scalar variables) where the specific message could not be categorized.","triggerScenarios":"Accessing `foo.bar` or `foo['bar']` in a template when foo is null, a string, or an int, with strict_variables enabled; rendering a template without passing a variable the template expects (it defaults to null); an optional relation that was not loaded; `{{ user.profile.name }}` when profile is null.","commonSituations":"Missing template context variables (forgot to pass them from the controller); nullable Doctrine relations accessed in a chain; API responses where an expected key is absent; strict_variables=true in the Twig environment (common in Symfony dev) surfacing bugs that silent null access would hide in prod.","solutions":["Pass the missing variable in the template context from the controller/action","Guard with the `default` filter or defined test: `{{ user.profile.name|default('') }}` or `{% if user.profile is defined and user.profile is not null %}`","Disable strict_variables if the null-access is intentional (not recommended; hides bugs)","Fix the data so the intermediate value is never null (initialize relations, provide fallbacks upstream)"],"exampleFix":"// before (Twig template)\n{{ user.profile.name }}\n// after\n{{ user.profile.name|default('unknown') }}","handlingStrategy":"try-catch","validationCode":"// PHP, before rendering\nforeach (['user', 'profile'] as $key) {\n    if (!array_key_exists($key, $context)) {\n        throw new \\LogicException(sprintf('Template context missing \"%s\".', $key));\n    }\n}","typeGuard":"// Twig-side narrowing using built-in tests\n{% if user is defined and user is not null and user.profile is defined and user.profile is not null %}\n    {{ user.profile.name }}\n{% endif %}","tryCatchPattern":"try {\n    $html = $twig->render('tpl.html.twig', $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (!str_starts_with($e->getMessage(), 'Impossible to access an attribute')) { throw $e; }\n    $html = $twig->render('tpl.html.twig', [...$context, 'user' => $fallbackUser]);\n}","preventionTips":["Always provide default context values for optional template variables","Use |default() or `is defined` guards on chained attribute access","Keep strict_variables=true in dev/CI to catch these before production","Initialize nullable Doctrine relations or check them before rendering","Validate that controller-rendered context matches the template's expected variables"],"tags":["twig","strict-variables","null","attribute-access"],"backgroundTag":"null-argument","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"}