You are here

protected function ComponentsLoader::findTemplate in Components! 3.x

Same name and namespace in other branches
  1. 8.2 src/Template/Loader/ComponentsLoader.php \Drupal\components\Template\Loader\ComponentsLoader::findTemplate()

Throws

\Twig\Error\LoaderError Thrown if a template matching $name cannot be found.

File

src/Template/Loader/ComponentsLoader.php, line 43

Class

ComponentsLoader
Loads namespaced templates from the filesystem.

Namespace

Drupal\components\Template\Loader

Code

protected function findTemplate($name, $throw = TRUE) {

  // Validate the given template.
  $extension = substr($name, strrpos($name, '.', -1));
  if ($name[0] !== '@' || !str_contains(substr($name, 2), '/') || $extension !== '.twig' && $extension !== '.html' && $extension !== '.svg') {
    if (!$throw) {
      return NULL;
    }
    throw new LoaderError(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name.twig").', $name));
  }
  else {

    // componentsRegistry::getTemplate() returns a string or NULL, exactly
    // what componentsLoader::findTemplate() should return.
    $path = $this->componentsRegistry
      ->getTemplate($name);
    if ($path || !$throw) {
      return $path;
    }
    throw new LoaderError(sprintf('Unable to find template "%s" in the components registry.', $name));
  }
}