PHPackages                             hkod/frontmatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. hkod/frontmatter

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

hkod/frontmatter
================

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture

1.1.0(8y ago)46051UnlicensePHPPHP &gt;=7.0

Since Feb 9Pushed 6y ago1 watchersCompare

[ Source](https://github.com/hanneskod/frontmatter)[ Packagist](https://packagist.org/packages/hkod/frontmatter)[ Docs](https://github.com/hanneskod/frontmatter)[ RSS](/packages/hkod-frontmatter/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

frontmatter
===========

[](#frontmatter)

[![Packagist Version](https://camo.githubusercontent.com/655756ed2b4f07739d36b629fb43d86b6233a91dde5bfd0007f9dc05dad125d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686b6f642f66726f6e746d61747465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hkod/frontmatter)[![Build Status](https://camo.githubusercontent.com/b04f5a9206c71c89d32a067afa507e253578d30dc6d233ede404b0e5795a8b72/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f68616e6e65736b6f642f66726f6e746d61747465722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/hanneskod/frontmatter)

A context aware frontmatter parser that supports multiple formats and uses a clean OOP architecture.

Front matter is metadata located at the top of a file wrapped in delimiting line tokens, usually `---`. The front matter may be formatted using YAML, Json or any ohter simliar format such as NEON or TOML.

Supported formats:

- Json
- INI
- YAML (`require symfony/yaml`)
- Markdown (`require erusev/parsedown`)
- Mustache (`require mustache/mustache`)

Parsers are simple callables, super easy to add more formats.

Installation
------------

[](#installation)

```
composer require hkod/frontmatter
```

Usage
-----

[](#usage)

A standard parser with yaml frontmatter and markdown body:

```
$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\YamlParser,
    new \hkod\frontmatter\MarkdownParser
);

$result = $parser->parse("---
key: value
---
This is a **template**
");

// value
echo $result->getFrontmatter()['key'];

// This is a template
echo $result->getBody();
```

### Specify the front matter delimiter

[](#specify-the-front-matter-delimiter)

> Note that the delimiting tokens always represents full lines.

You may set the delimiters when creating the block parser.

```
$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\BlockParser('***', '***')
);

$result = $parser->parse("***
frontmatter
***
body
");

// frontmatter
echo $result->getFrontmatter();
```

### Putting the frontmatter last

[](#putting-the-frontmatter-last)

> Note that since the delimiting tokens represent a line the last line must end whit a new line (or similar) or it won't be recognized by the parser.

*Frontmatter* also supports an inverted block parser, where the frontmatter is expected to bee last instead of first.

```
$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\InvertedBlockParser
);

$result = $parser->parse("
This is a the body
---
This is the frontmatter
---
");

// "This is the frontmatter"
echo $result->getFrontmatter();
```

### Passing a context

[](#passing-a-context)

When parsing you may pass a context to the parser and it will in turn be passed along to all subsequent parsers. Context dependet parsers may for example expand templates...

```
$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\MustacheParser
);

$context = ['variable' => 'foobar'];

$result = $parser->parse("{{variable}}", $context);

// foobar
echo $result->getBody();
```

### Creating complex parsers

[](#creating-complex-parsers)

```
$parser = (new \hkod\frontmatter\ParserBuilder)
    ->addFrontmatterPass(new \hkod\frontmatter\MustacheParser)
    ->addFrontmatterPass(new \hkod\frontmatter\YamlParser)
    ->addBodyPass(new \hkod\frontmatter\MustacheParser)
    ->addBodyPass(new \hkod\frontmatter\MarkdownParser)
    ->setBlockParser(new \hkod\frontmatter\BlockParser('***', '***'))
    ->buildParser();

$document = "***
key: {{variable}}
***
This is a **{{text}}** template
";

$context = ['variable' => 'value', 'text' => 'markdown'];

$result = $parser->parse($document, $context);

// value
echo $result->getFrontmatter()['key'];

// This is a markdown template
echo $result->getBody();
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

3015d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0af6567c7f4f5518a422192675445464fdcf18acbc9459b3205da9fd446f8d0?d=identicon)[hanneskod](/maintainers/hanneskod)

---

Top Contributors

[![hanneskod](https://avatars.githubusercontent.com/u/1369274?v=4)](https://github.com/hanneskod "hanneskod (22 commits)")

---

Tags

frontmatterinijsonmarkdownmustacheyamlmetadatafrontmatter

### Embed Badge

![Health badge](/badges/hkod-frontmatter/health.svg)

```
[![Health](https://phpackages.com/badges/hkod-frontmatter/health.svg)](https://phpackages.com/packages/hkod-frontmatter)
```

###  Alternatives

[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[mck89/peast

Peast is PHP library that generates AST for JavaScript code

18834.7M29](/packages/mck89-peast)[ivopetkov/html5-dom-document-php

HTML5 DOMDocument PHP library (extends DOMDocument)

6031.4M49](/packages/ivopetkov-html5-dom-document-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
