FrontMatter example


  public function getSourceContext(string $name): Source {
    $path = $this->findTemplate($name);

    $contents = file_get_contents($path);
    try {
      // 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.       $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()));
    }
Home | Imprint | This part of the site doesn't use cookies.