Skip to content

Repository files navigation

Front Matter

PackagistQuality Gate StatusCoverage

The most universal Front matter (yaml, json, neon, toml) parser and dumper for PHP. Front matter allows page-specific variables to be included at the top of a page.

Features

This library can parse all form of front matter:

YAML (Neon)TOMLJsonTwigPug
---foo: bar---# Section
Text
+++foo = bar+++# Section
Text
{
"foo": "bar"
}
# Section
Text
{#---
foo: bar
---#}
<h1>Section</h1>
<p>{{ foo }}</p>
--
foo: bar
h1 Section
p= foo

Installation

This library can be installed via Composer:

composer require webuni/front-matter

Usage

Automatic front matter detection and parsing

The following code will automatically detect the above front matter types:

<?php$frontMatter = \Webuni\FrontMatter\FrontMatterChain::create();
$document = $frontMatter->parse($string);
$data = $document->getData();
$content = $document->getContent();

Parse an arbitrary string

<?php$frontMatter = new \Webuni\FrontMatter\FrontMatter();
$document = $frontMatter->parse($string);
$data = $document->getData();
$content = $document->getContent();

Check if a string has front matter

<?php$frontMatter = new \Webuni\FrontMatter\FrontMatter();
$hasFrontMatter = $frontMatter->exists($string);

Twig loader

If you want to store metadata about twig template, e.g.:

{#---title: Hello worldmenu: mainweight: 20---#}
{% extendlayout.html.twig %}
{% blockcontent %}
Hello world!
{% endblock %}

you can use FrontMatterLoader, that decorates another Twig loader:

$frontMatter = \Webuni\FrontMatter\Twig\TwigCommentFrontMatter::create();
$loader = new \Twig\Loader\FilesystemLoader(['path/to/templates']);
$loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader);
$twig = new \Twig\Environment($loader);
$content = $twig->render('template', []);
// rendered the valid twig template without front matter

It is possible to inject front matter to Twig template as variables:

// …$converter = \Webuni\FrontMatter\Twig\DataToTwigConvertor::vars();
$loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader, $converter);
// …

Loaded Twig template has this code:

{% settitle="Hello world" %}
{% setmenu="main" %}
{% setweight=20 %}
{% line5 %}
{% extendlayout.html.twig %}
{% blockcontent %}
Hello world!
{% endblock %}

Available converters:

ConverterTwig
DataToTwigConvertor::nothing()
DataToTwigConvertor::vars(){% set key1 = value1 %}
DataToTwigConvertor::vars(false){% set key1 = key1 is defined ? key1 : value1 %}
DataToTwigConvertor::var('name'){% set name = {key1: value1, key2: value2} %}
DataToTwigConvertor::var('name', false){% set name = name is defined ? name : {key1: value1} %}

Markdown

The most commonly used front matter is for markdown files:

---layout: posttitle: I Love Markdowntags:
- test
- example---# Hello World!

This library can be used with league/commonmark:

$frontMatter = new \Webuni\FrontMatter\FrontMatter();
$extension = new \Webuni\FrontMatter\Markdown\FrontMatterLeagueCommonMarkExtension($frontMatter);
$converter = new \League\CommonMark\CommonMarkConverter([]);
$converter->getEnvironment()->addExtension($extension);
$html = $converter->convertToHtml('markdown'); // html without front matter

Alternatives

About

The most featured front matter (yaml, json, neon, toml) parser and dumper for PHP.

Topics

Resources

Stars

39 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages