Install spatie/yaml-front-matter via Composer
mainTo use this package in your PHP project, install it using Composer:
$ composer require spatie/yaml-front-matterrepository·main·Indexed 43 days ago
https://github.com/spatie/yaml-front-matterA lightweight PHP package for parsing YAML front matter from files and strings. It separates metadata wrapped in triple dashes from the main document body, providing the YamlFrontMatter::parse(), YamlFrontMatter::parseFile(), and YamlFrontMatter::markdownCompatibleParse() methods to extract data into a Document object.
To use this package in your PHP project, install it using Composer:
$ composer require spatie/yaml-front-matterUse the YamlFrontMatter::parse() method to extract metadata and the body from a string containing YAML front matter (wrapped in --- delimiters).
```php
use Spatie\\The YamlFrontMatter::parse() method returns a YamlFrontMatterObject. You can interact with this object to retrieve the parsed metadata or the file body using several methods and property accessors:
$object->matter(): Returns all parsed front matter as an associative array.$object->matter('key'): Returns the value for a specific key. Supports dot notation for nested fields (e.g., $object->matter('meta.keywords')).$object->body(): Returns the content of the file excluding the front matter.$object->title).use Spatie\YamlFrontMatter\YamlFrontMatter;
$object = YamlFrontMatter::parse("---\ntitle: Example\n---\n\nLorem ipsum.");
$object->matter(); // => ['title' => 'Example']
$object->matter('title'); // => 'Example'
$object->matter('meta.id'); // => Supports dot notation for nested YAML
$object->body(); // => 'Lorem ipsum.'
$object->title; // => 'Example';Use YamlFrontMatter::parseFile() to read a file from a given path and parse its YAML front matter. This is a convenience method that wraps file_get_contents() and parse().
use Spatie\YamlFrontMatter\YamlFrontMatter;
$document = YamlFrontMatter::parseFile(__DIR__ . '/example.md');Standard parsing might fail or behave unexpectedly if the body of your document contains its own --- delimiters (e.g., code blocks or nested Markdown). Use YamlFrontMatter::markdownCompatibleParse() to use the ComplexMarkdownParser, which is designed to handle Markdown files that contain nested Markdown structures correctly.
use Spatie\YamlFrontMatter\YamlFrontMatter;
$document = YamlFrontMatter::markdownCompatibleParse($content);Use YamlFrontMatter::parse() to extract YAML front matter and the remaining content from a string. The method looks for content delimited by --- separators. If no valid front matter is found, it returns a Document object containing an empty array for the matter and the original string as the body.
use Spatie\YamlFrontMatter\YamlFrontMatter;
$document = YamlFrontMatter::parse("---\nfoo: bar\n---\n\nHello world!");
$matter = $document->matter(); // ['foo' => 'bar']
$body = $document->body(); // "\nHello world!"The Document class represents a parsed file containing YAML front matter and a body. You can retrieve the entire front matter array, specific keys using dot notation, or the raw file body.
matter() with no arguments.matter('key_name') or access it as a dynamic property $document->key_name.body().When using matter($key, $default), if the key is not found, it returns the provided $default value.
// Assuming $document is an instance of Spatie\YamlFrontMatter\Document
// Get all front matter
$allMatter = $document->matter();
// Get a specific key with a default value
$title = $document->matter('title', 'Untitled');
// Access via dynamic property
$author = $document->author;
// Get the file body
$content = $document->body();The matter() method allows you to retrieve specific values from the YAML front matter. It supports dot notation for nested keys via Arr::get.
Parameters:
?string $key: The key to retrieve. Supports dot notation (e.g., 'author.name'). If null, returns the entire matter array.mixed $default: The value to return if the key does not exist.// Retrieve nested value
$name = $document->matter('author.name', 'Unknown');
// Retrieve top-level value
$date = $document->matter('date');The body() method returns the content of the file that exists after the YAML front matter block.
$content = $document->body();