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
- Host: GitHub
- URL: https://github.com/carlcraig/tc-front-matter
- Owner: carlcraig
- License: mit
- Created: 2017-03-03T23:15:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T02:48:09.000Z (over 9 years ago)
- Last Synced: 2025-07-31T15:01:25.656Z (11 months ago)
- Topics: front-matter, json, php, symfony, yaml
- Language: PHP
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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