Change record status: 
Project: 
Introduced in branch: 
9.1.x
Introduced in version: 
9.1.0-alpha1
Description: 

A new Drupal\Component\FrontMatter\FrontMatter component has been added to Drupal.

This component allows for an easy and convenient way to parse front matter from a source.

Front matter is used as a way to provide additional static data associated with a source without affecting the contents of the source. Typically this is used in templates to denote special handling or categorization.

Front matter must be the first thing in the source and must take the form of valid YAML set in between triple-hyphen lines:

source.md:

---
important: true
---
My content

example.php:

use Drupal\Component\FrontMatter\FrontMatter;

$frontMatter = FrontMatter::create(file_get_contents('source.md'));
$data = $frontMatter->getData(); // ['important' => TRUE]
$content = $frontMatter->getContent(); // 'My content'
$line => $frontMatter->getLine(); // 4, line where content actually starts.

Twig has been extended in Drupal to provide an easy way to parse front matter from template files.

$metadata = \Drupal::service('twig')->getTemplateMetadata('/path/to/template.html.twig');
NOTE All front matter is stripped from Twig templates prior to rendering.
Impacts: 
Module developers