LoaderError example

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)));
    }

    public function setLexer(Lexer $lexer)
    {
        $this->lexer = $lexer;
    }

    /** * @throws SyntaxError When the code is syntactically wrong */
    public function tokenize(Source $source): TokenStream
    {

  protected function findTemplate(string $name, bool $throw = TRUE): ?string {
    $path = $name;
    try {
      $component = $this->pluginManager->find($name);
      $path = $component->getTemplatePath();
    }
    catch (ComponentNotFoundException $e) {
      if ($throw) {
        throw new LoaderError($e->getMessage()$e->getCode()$e);
      }
    }
    if ($path || !$throw) {
      return $path;
    }

    throw new LoaderError(sprintf('Unable to find template "%s" in the components registry.', $name));
  }

  /** * {@inheritdoc} */

      // Customize the list of extensions if no file extension is allowed.       $extensions = $this->allowedFileExtensions;
      $no_extension = array_search('', $extensions, TRUE);
      if (is_int($no_extension)) {
        unset($extensions[$no_extension]);
        $extensions[] = 'or no file extension';
      }
      if (empty($extension)) {
        $extension = 'no file extension';
      }
      throw new LoaderError(sprintf("Template %s has an invalid file extension (%s). Only templates ending in one of %s are allowed. Set the twig.config.allowed_file_extensions container parameter to customize the allowed file extensions", $name$extensionimplode(', ', $extensions)));
    }

    // Previously it was possible to access files in the parent directory of a     // namespace. This was removed in Twig 2.15.3. In order to support backwards     // compatibility, we are adding path directory as a namespace, and therefore     // we can remove the directory traversal from the name.     // @todo deprecate this functionality for removal in Drupal 11.     if (preg_match('/(^\@[^\/]+\/)\.\.\/(.*)/', $name$matches)) {
      $name = $matches[1] . $matches[2];
    }

    
/** * @throws LoaderError */
    public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE): void
    {
        // invalidate the cache         $this->cache = $this->errorCache = [];

        $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
        if (!is_dir($checkPath)) {
            throw new LoaderError(sprintf('The "%s" directory does not exist ("%s").', $path$checkPath));
        }

        $this->paths[$namespace][] = rtrim($path, '/\\');
    }

    /** * @throws LoaderError */
    public function prependPath(string $path, string $namespace = self::MAIN_NAMESPACE): void
    {
        // invalidate the cache
      $front_matter = new FrontMatter($contents, Yaml::class);

      // Reconstruct the content if there is front matter data detected. Prepend       // the source with {% line \d+ %} to inform Twig that the source code       // actually starts on a different line past the front matter data. This is       // particularly useful when used in error reporting.       if ($front_matter->getData() && ($line = $front_matter->getLine())) {
        $contents = "{% line $line %}" . $front_matter->getContent();
      }
    }
    catch (InvalidDataTypeException $e) {
      throw new LoaderError(sprintf('Malformed YAML in help topic "%s": %s.', $path$e->getMessage()));
    }

    return new Source($contents$name$path);
  }

  /** * {@inheritdoc} */
  protected function findTemplate($name$throw = TRUE) {
    if (!str_ends_with($name, '.html.twig')) {
      if (!$throw) {
        
$path = $info['path'] . '/' . $name;
      }
      elseif (isset($info['template'])) {
        $path = $info['template'] . '.html.twig';
      }
      if (isset($path) && is_file($path)) {
        return $this->cache[$name] = $path;
      }
    }

    if ($throw) {
      throw new LoaderError(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));
    }

    return NULL;
  }

  /** * {@inheritdoc} */
  public function getCacheKey(string $name): string {
    // The parent implementation does unnecessary work that triggers     // deprecations in PHP 8.1.
$this->templates = $templates;
    }

    public function setTemplate(string $name, string $template): void
    {
        $this->templates[$name] = $template;
    }

    public function getSourceContext(string $name): Source
    {
        if (!isset($this->templates[$name])) {
            throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
        }

        return new Source($this->templates[$name]$name);
    }

    public function exists(string $name): bool
    {
        return isset($this->templates[$name]);
    }

    public function getCacheKey(string $name): string
    {
if (!$loader->exists($name)) {
                continue;
            }

            try {
                return $loader->getSourceContext($name);
            } catch (LoaderError $e) {
                $exceptions[] = $e->getMessage();
            }
        }

        throw new LoaderError(sprintf('Template "%s" is not defined%s.', $name$exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
    }

    public function exists(string $name): bool
    {
        if (isset($this->hasSourceCache[$name])) {
            return $this->hasSourceCache[$name];
        }

        foreach ($this->loaders as $loader) {
            if ($loader->exists($name)) {
                return $this->hasSourceCache[$name] = true;
            }
Home | Imprint | This part of the site doesn't use cookies.