twigphp/Twig · error · Twig\Error\LoaderError

Unable to find one of the following templates

Error message

Unable to find one of the following templates: "%s".

What it means

Environment::resolveTemplate throws this Twig\LoaderError when none of the names passed (an array of template candidates) could be loaded by the configured loader. It is the multi-candidate variant of 'template not found': every entry in the array was tried and each failed.

Solutions

  1. Check that at least one of the listed names exists under a registered loader path; fix the name or create the template.
  2. Verify loader configuration: addPath() directories and register()ed namespaces actually cover the candidate names.
  3. Dump loader paths ($loader->getPaths() / getNamespaces()) and compare with the names passed.
  4. On case-sensitive filesystems, confirm the file name's case matches exactly.
  5. If using ArrayLoader or a custom loader, confirm its cache/source map contains the keys.

Example fix

// before
$loader = new FilesystemLoader('/wrong/dir');
$twig->resolveTemplate(['base.html.twig', 'fallback.html.twig']);

// after
$loader = new FilesystemLoader(__DIR__.'/templates');
$twig->resolveTemplate(['base.html.twig', 'fallback.html.twig']);
Defensive patterns

Strategy: validation

Validate before calling

$loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/templates');
foreach ($names as $n) {
    if (!$loader->exists($n)) {
        error_log("Template candidate missing: $n (paths: ".implode(',', $loader->getPaths()).')');
    }
}
if ($loader->exists($names[0])) { $twig->resolveTemplate($names); }

Try / catch

try {
    $tpl = $twig->resolveTemplate(['theme/x.html.twig', 'default/x.html.twig']);
} catch (\Twig\Error\LoaderError $e) {
    $tpl = $twig->resolveTemplate('fallback.html.twig');
}

Prevention

When it happens

Trigger: Calling $twig->resolveTemplate(['a.html.twig', 'b.html.twig']) or rendering with an array of template names where no name exists according to the loader (FilesystemLoader paths misconfigured, wrong namespace, wrong extension).

Common situations: Twig\FilesystemLoader addPath pointing at the wrong directory; using @namespace/name while the namespace was never registered; rendering an array of theme fallbacks where all candidates were removed/renamed; case-sensitivity mismatches on Linux.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Environment.php:516

        $count = \count($names);
        foreach ($names as $name) {
            if ($name instanceof Template) {
                trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', Template::class, __METHOD__);

                return new TemplateWrapper($this, $name);
            }
            if ($name instanceof TemplateWrapper) {
                return $name;
            }

            if (1 !== $count && !$this->getLoader()->exists($name)) {
                continue;
            }

            return $this->load($name);
        }

        throw new LoaderError(\sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));
    }

    /**
     * @return void
     */
    public function setLexer(Lexer $lexer)
    {
        $this->lexer = $lexer;
    }

    /**
     * @throws SyntaxError When the code is syntactically wrong
     */
    public function tokenize(Source $source): TokenStream
    {
        if (null === $this->lexer) {
            $this->lexer = new Lexer($this);
        }

View on GitHub (pinned to a414c3a491)