PHPackages                             ergebnis/twig-front-matter - 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. [Templating &amp; Views](/categories/templating)
4. /
5. ergebnis/twig-front-matter

ActiveLibrary[Templating &amp; Views](/categories/templating)

ergebnis/twig-front-matter
==========================

Provides a Twig loader for files with YAML front-matter.

1.3.0(9mo ago)230.3k↓50%[15 PRs](https://github.com/ergebnis/twig-front-matter/pulls)MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0

Since Jul 7Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/ergebnis/twig-front-matter)[ Packagist](https://packagist.org/packages/ergebnis/twig-front-matter)[ Docs](https://github.com/ergebnis/twig-front-matter)[ RSS](/packages/ergebnis-twig-front-matter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (18)Versions (23)Used By (0)

twig-front-matter
=================

[](#twig-front-matter)

[![Integrate](https://github.com/ergebnis/twig-front-matter/workflows/Integrate/badge.svg)](https://github.com/ergebnis/twig-front-matter/actions)[![Merge](https://github.com/ergebnis/twig-front-matter/workflows/Merge/badge.svg)](https://github.com/ergebnis/twig-front-matter/actions)[![Release](https://github.com/ergebnis/twig-front-matter/workflows/Release/badge.svg)](https://github.com/ergebnis/twig-front-matter/actions)[![Renew](https://github.com/ergebnis/twig-front-matter/workflows/Renew/badge.svg)](https://github.com/ergebnis/twig-front-matter/actions)

[![Code Coverage](https://camo.githubusercontent.com/1d9f26cf47b1ad4ead154d0b5fd75a6138ea0ba56f858c3fc745159b6c3369f7/68747470733a2f2f636f6465636f762e696f2f67682f65726765626e69732f747769672d66726f6e742d6d61747465722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/ergebnis/twig-front-matter)

[![Latest Stable Version](https://camo.githubusercontent.com/b5eb5745a527db91bd738d6db9767188440832306a65a9807131844c56446a05/68747470733a2f2f706f7365722e707567782e6f72672f65726765626e69732f747769672d66726f6e742d6d61747465722f762f737461626c65)](https://packagist.org/packages/ergebnis/twig-front-matter)[![Total Downloads](https://camo.githubusercontent.com/eb5058481d24fb94e6e95a9dbb108ee69cc0c877aff4b72a4635874298f152dd/68747470733a2f2f706f7365722e707567782e6f72672f65726765626e69732f747769672d66726f6e742d6d61747465722f646f776e6c6f616473)](https://packagist.org/packages/ergebnis/twig-front-matter)[![Monthly Downloads](https://camo.githubusercontent.com/561abad6e360c05c41e81e230c7a13e2c3d9cac508e727b966f82af322b82173/687474703a2f2f706f7365722e707567782e6f72672f65726765626e69732f747769672d66726f6e742d6d61747465722f642f6d6f6e74686c79)](https://packagist.org/packages/ergebnis/twig-front-matter)

This project provides a [`composer`](https://getcomposer.org) package with a [Twig](https://twig.symfony.com) loader for files with [YAML front-matter](https://github.com/ergebnis/front-matter).

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

[](#installation)

Run

```
composer require ergebnis/twig-front-matter
```

Usage
-----

[](#usage)

### Loading Twig templates with the `FrontMatterLoader`

[](#loading-twig-templates-with-the-frontmatterloader)

This project ships with a [`FrontMatterLoader`](/src/FrontMatterLoader.php) that you can use to load Twig templates with YAML front-matter.

The `FrontMatterLoader`

- parses a Twig template and separates the front-matter from the body-matter using [`ergebnis/front-matter](https://github.com/ergebnis/front-matter)
- converts the front-matter data to Twig assignments using an implementation of [`Converter\FrontMatterConverter`](/src/Converter/FrontMatterConverter.php)
- returns a new Twig Source that merges the Twig assignments from the front-matter data with the body-matter from the Twig template

### YAML front-matter

[](#yaml-front-matter)

Assume that you have a Twig template with the following YAML front-matter:

```
foo: bar
number: 1234
pi: 3.14159
date: 2016-05-27
empty: ~
invalid-key: "hmm"
multiline: |
  Multiple
  Line
  String
object:
  key: value
  datetime: 2020-11-12 12:54:12
  values:
    - one
    - two
```

### Converting YAML front-matter to Twig assignments with the `Converter/ToMultipleAssignmentsFrontMatterConverter`

[](#converting-yaml-front-matter-to-twig-assignments-with-the-convertertomultipleassignmentsfrontmatterconverter)

This project ships with a [`Converter/ToMultipleAssignmentsFrontMatterConverter.php`](/src/Converter/ToMultipleAssignmentsFrontMatterConverter.php) that you can use to convert the YAML front-matter to multiple Twig assignments.

The example below will convert the parsed YAML front-matter data to multiple Twig assignments that will assign data to Twig variables with force:

```
declare(strict_types=1);

use Ergebnis\Twig;

$frontMatterConverter = new Twig\FrontMatter\Converter\ToMultipleAssignmentsFrontMatterConverter(true);

echo $frontMatterConverter->convert($data);
```

```
{% set foo = "bar" %}
{% set number = 1234 %}
{% set pi = 3.14159 %}
{% set date = (1464307200|date_modify('0sec')) %}
{% set empty = null %}
{% set multiline = "Multiple\nLine\nString\n" %}
{% set object = { key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } } %}
```

The example below will convert the parsed YAML front-matter data to multiple Twig assignments that will assign data to Twig variables without force (taking into account that you may pass variables to the template and prefer not to override these variables with front-matter):

```
declare(strict_types=1);

use Ergebnis\Twig;

$frontMatterConverter = new Twig\FrontMatter\Converter\ToMultipleAssignmentsFrontMatterConverter(false);

echo $frontMatterConverter->convert($data);
```

```
{% set foo = foo is defined ? foo : "bar" %}
{% set number = number is defined ? number : 1234 %}
{% set pi = pi is defined ? pi : 3.14159 %}
{% set date = date is defined ? date : (1464307200|date_modify('0sec')) %}
{% set empty = empty is defined ? empty : null %}
{% set multiline = multiline is defined ? multiline : "Multiple\nLine\nString\n" %}
{% set object = object is defined ? object|merge({ key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } }) : { key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } } %}
```

### Converting YAML front-matter to a Twig assignment with the `Converter/ToSingleAssignmentFrontMatterConverter`

[](#converting-yaml-front-matter-to-a-twig-assignment-with-the-convertertosingleassignmentfrontmatterconverter)

This project ships with a [`Converter/ToSingleAssignmentFrontMatterConverter.php`](/src/Converter/ToSingleAssignmentFrontMatterConverter.php) that you can use to convert the YAML front-matter to a single Twig assignment.

The example below will convert the parsed YAML front-matter data to a single Twig assignment that will assign data to a Twig variable with force:

```
declare(strict_types=1);

use Ergebnis\Twig;

$frontMatterConverter = new Twig\FrontMatter\Converter\ToSingleAssignmentFrontMatterConverter(
    Twig\Expression\Name::fromString('data'),
    false,
);

echo $frontMatterConverter->convert($data);
```

```
{% set data = { foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, invalid-key: "hmm", multiline: "Multiple\\nLine\\nString\\n", object: { key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } } } %}
```

The example below will convert the parsed YAML front-matter data to a single Twig assignment that will assign data to a Twig variables without force (taking into account that you may pass a variable to the template and prefer not to override this variable with front-matter):

```
declare(strict_types=1);

use Ergebnis\Twig;

$frontMatterConverter = new Twig\FrontMatter\Converter\ToSingleAssignmentFrontMatterConverter(
    Twig\Expression\Name::fromString('data'),
    false,
);

echo $frontMatterConverter->convert($data);
```

```
{% set data = data is defined ? data|merge({ foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, invalid-key: "hmm", multiline: "Multiple\\nLine\\nString\\n", object: { key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } } }) : { foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, invalid-key: "hmm", multiline: "Multiple\\nLine\\nString\\n", object: { key: "value", datetime: (1605185652|date_modify('0sec')), values: { 0: "one", 1: "two" } } } %}
```

### Configuring services in a Symfony project

[](#configuring-services-in-a-symfony-project)

Adjust your `config/services.php` as follows to register a `FrontMatterLoader` with a `Converter\ToMultipleAssignmentsFrontMatterConverter`:

```
