twigphp/Twig · error · LoaderError

Template " " is not defined.

Error message

Template "%s" is not defined.

What it means

findTemplate() keeps an errorCache of previously failed lookups per template name. When a name is looked up again after a prior failure, this cached error message ('Template "%s" is not defined.') is rethrown instead of re-scanning, reporting that the template could not be found in any registered path for its namespace.

Solutions

  1. Create the template file in one of the registered paths or fix its name.
  2. Check registered paths with getPaths($namespace) and add the correct one via addPath().
  3. Instantiate a fresh loader (or call setPaths) after config changes to clear the internal cache.

Example fix

// before
$twig->render('emial/welcome.html.twig'); // typo
// after
$twig->render('email/welcome.html.twig');
Defensive patterns

Strategy: validation

Validate before calling

foreach ($templates as $t) { if (!$loader->exists($t) && !file_exists($candidatePath)) { warn("template $t unresolvable"); } }

Try / catch

try { $twig->render($name, $ctx); } catch (Twig\Error\LoaderError $e) { // check errorCache message; fix name or add path and recreate loader }

Prevention

When it happens

Trigger: Looking up a template name (via getSourceContext, getCacheKey, exists, isFresh) that failed before; the name has no matching file in any registered path, or the errorCache holds a stale failure from before a path was added without cache invalidation.

Common situations: Typo in template filename; template in wrong namespace; tests reusing a loader after paths changed (cache not invalidated); template deleted after a first failed lookup.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/1100ded1b96f35a4. Report an issue: GitHub.

Appendix: source

Thrown at src/Loader/FilesystemLoader.php:189

    }

    /**
     * @return string|null
     */
    protected function findTemplate(string $name, bool $throw = true)
    {
        $name = $this->normalizeName($name);

        if (isset($this->cache[$name])) {
            return $this->cache[$name];
        }

        if (isset($this->errorCache[$name])) {
            if (!$throw) {
                return null;
            }

            throw new LoaderError($this->errorCache[$name]);
        }

        try {
            [$namespace, $shortname] = $this->parseName($name);

            $this->validateName($shortname);
        } catch (LoaderError $e) {
            if (!$throw) {
                return null;
            }

            throw $e;
        }

        if (!isset($this->paths[$namespace])) {
            $this->errorCache[$name] = \sprintf('There are no registered paths for namespace "%s".', $namespace);

            if (!$throw) {

View on GitHub (pinned to a414c3a491)