{"record":{"id":"a9936df4aafc9268","repo":"twigphp/Twig","slug":"template-s-is-not-defined","errorCode":null,"errorMessage":"Template \"%s\" is not defined.","messagePattern":"Template \"(.+?)\" is not defined\\.","errorType":"exception","errorClass":"LoaderError","httpStatus":null,"severity":"error","filePath":"src/Loader/ArrayLoader.php","lineNumber":47,"sourceCode":"final class ArrayLoader implements LoaderInterface\n{\n    /**\n     * @param array $templates An array of templates (keys are the names, and values are the source code)\n     */\n    public function __construct(\n        private array $templates = [],\n    ) {\n    }\n\n    public function setTemplate(string $name, string $template): void\n    {\n        $this->templates[$name] = $template;\n    }\n\n    public function getSourceContext(string $name): Source\n    {\n        if (!isset($this->templates[$name])) {\n            throw new LoaderError(\\sprintf('Template \"%s\" is not defined.', $name));\n        }\n\n        return new Source($this->templates[$name], $name);\n    }\n\n    public function exists(string $name): bool\n    {\n        return isset($this->templates[$name]);\n    }\n\n    public function getCacheKey(string $name): string\n    {\n        if (!isset($this->templates[$name])) {\n            throw new LoaderError(\\sprintf('Template \"%s\" is not defined.', $name));\n        }\n\n        return $name.':'.$this->templates[$name];\n    }","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Loader/ArrayLoader.php#L29-L65","documentation":"ArrayLoader::getSourceContext throws a LoaderError when asked for the source of a template name that was never set on the in-memory templates array. ArrayLoader only knows templates registered via setTemplate() or the constructor, so any unknown name fails immediately.","triggerScenarios":"Calling $loader->getSourceContext('name') (directly or via $twig->load()/render()) for a name not passed to the ArrayLoader constructor or setTemplate(). Called in tests like testGetSourceContextWhenTemplateDoesNotExist.","commonSituations":"Typos in template names passed to $twig->render(); forgetting to register a template in the loader before rendering; environment switches (dev registers templates, prod doesn't); refactors renaming template keys without updating render calls.","solutions":["Register the template: $loader->setTemplate('name', $source) or include it in the ArrayLoader constructor array.","Check the name for typos and exact-case match against registered keys.","Guard with $loader->exists($name) before calling getSourceContext.","If templates should come from disk, use FilesystemLoader instead of ArrayLoader."],"exampleFix":"// before\n$loader = new ArrayLoader();\necho $twig->render('index.html');\n\n// after\n$loader = new ArrayLoader(['index.html' => '<h1>Hi</h1>']);\necho $twig->render('index.html');","handlingStrategy":"try-catch","validationCode":"if (!$loader->exists($name)) {\n    throw new \\RuntimeException(\"Template '{$name}' is not registered in the ArrayLoader\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $source = $loader->getSourceContext($name);\n} catch (\\Twig\\Error\\LoaderError $e) {\n    log(\"Unknown template '{$name}': registered keys: \".implode(', ', array_keys($loader->getCacheKeys ? $loader->getCacheKeys() : [])));\n    throw $e;\n}","preventionTips":["Always register templates in the loader before rendering.","Use $loader->exists() as a guard for user-supplied template names.","Keep a single registry/constants file for template names to avoid typos.","Use FilesystemLoader when templates live on disk."],"tags":["loader","template","not-found","twig"],"backgroundTag":"template-not-found","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"}