You are here

public function TwigEnvironment::getTemplateMetadata in Drupal 9

Retrieves metadata associated with a template.

Parameters

string $name: The name for which to calculate the template class name.

Return value

array The template metadata, if any.

Throws

\Twig\Error\LoaderError

\Twig\Error\SyntaxError

File

core/lib/Drupal/Core/Template/TwigEnvironment.php, line 168

Class

TwigEnvironment
A class that defines a Twig environment for Drupal.

Namespace

Drupal\Core\Template

Code

public function getTemplateMetadata(string $name) : array {
  $loader = $this
    ->getLoader();
  $source = $loader
    ->getSourceContext($name);

  // Note: always use \Drupal\Core\Serialization\Yaml here instead of the
  // "serializer.yaml" service. This allows the core serializer to utilize
  // core related functionality which isn't available as the standalone
  // component based serializer.
  try {
    return FrontMatter::create($source
      ->getCode(), Yaml::class)
      ->getData();
  } catch (FrontMatterParseException $exception) {

    // Convert parse exception into a syntax exception for Twig and append
    // the path/name of the source to help further identify where it occurred.
    $message = sprintf($exception
      ->getMessage() . ' in %s', $source
      ->getPath() ?: $source
      ->getName());
    throw new SyntaxError($message, $exception
      ->getSourceLine(), $source, $exception);
  }
}