PHP code example of tc / front-matter

1. Go to this page and download the library: Download tc/front-matter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tc / front-matter example snippets




use Tc\FrontMatter\FrontMatter;
use Tc\FrontMatter\Adapter\JsonAdapter;

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

// create a new parser/dumper
$frontMatter = new FrontMatter();

// parse file contents
$document = $frontMatter->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);



namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $file = file_get_contents('./path/to/front-matter-file');
        $document = $this->get('tc.front_matter')->parse($file);

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



$myFrontMatter = new FrontMatter(new FooAdapter());