function TwigEnvironment::getTemplateMetadata

Same name in other branches
  1. 9 core/lib/Drupal/Core/Template/TwigEnvironment.php \Drupal\Core\Template\TwigEnvironment::getTemplateMetadata()
  2. 10 core/lib/Drupal/Core/Template/TwigEnvironment.php \Drupal\Core\Template\TwigEnvironment::getTemplateMetadata()

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 167

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.