An open API service indexing awesome lists of open source software.

https://github.com/carlcraig/tc-front-matter

An adapter based front matter parser/dumper for php
https://github.com/carlcraig/tc-front-matter

front-matter json php symfony yaml

Last synced: 5 months ago
JSON representation

An adapter based front matter parser/dumper for php

Awesome Lists containing this project

README

          

Tc Front Matter
===============

> An adapter based front matter parser/dumper for php

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

```shell
composer require tc/front-matter
```

Adapters
--------

- YAML
- JSON

Example Usage
-------------

```php
parse($fileContent);

// get data
$document->getData();

// get content
$document->getContent();

// dump the document back to front matter string
$dump = $frontMatter->dumpDocument($document);

// dump data and content back to front matter string
$dump = $frontMatter->dump(['foo' => 'bar'], 'Hello World');

// parse JSON front matter
$jsonAdapter = new JsonAdapter();

// create new parser/dumper using the json adaptor
$frontMatter = new FrontMatter($jsonAdapter);

// sample json front matter
$fileContent = '---
{
"title": "A Title",
"slug": "a-slug",
"created": "2017-01-01 12:00"
}
---
This is some sample content
';

// parse file contents
$document = $frontMatter->parse($fileContent);
```

Symfony Integration
-------------------

To enable symfony integration add the bundle to your kernel

```
new Tc\FrontMatter\Bridge\Symfony\TcFrontMatterBundle(),
```

Now you have access to the front matter service's:

- YAML `tc.front_matter` or `tc.front_matter.yaml`
- JSON `tc.front_matter.json`

Example:

```php
get('tc.front_matter')->parse($file);

return $this->render('default/index.html.twig', [
'content' => $document->getContent(),
'data' => $document->getData()
]);
}
}
```

Custom Adapters
---------------

You can create your own custom adapters to parse and dump front matter.

All you need to do is implement `Tc\FrontMatter\Adapter\AdapterInterface`

You can then create a new instance of front matter using your adapter. e.g.

```php