PHP code example of hkod / frontmatter

1. Go to this page and download the library: Download hkod/frontmatter 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/ */

    

hkod / frontmatter example snippets


$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\YamlParser,
    new \hkod\frontmatter\MarkdownParser
);

$result = $parser->parse("---
key: value
---
This is a **template**
");

// value
echo $result->getFrontmatter()['key'];

// <p>This is a <strong>template</strong></p>
echo $result->getBody();

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\BlockParser('***', '***')
);

$result = $parser->parse("***
frontmatter
***
body
");

// frontmatter
echo $result->getFrontmatter();

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\InvertedBlockParser
);

$result = $parser->parse("
This is a the body
---
This is the frontmatter
---
");

// "This is the frontmatter"
echo $result->getFrontmatter();

$parser = new \hkod\frontmatter\Parser(
    new \hkod\frontmatter\VoidParser,
    new \hkod\frontmatter\MustacheParser
);

$context = ['variable' => 'foobar'];

$result = $parser->parse("{{variable}}", $context);

// foobar
echo $result->getBody();

$parser = (new \hkod\frontmatter\ParserBuilder)
    ->addFrontmatterPass(new \hkod\frontmatter\MustacheParser)
    ->addFrontmatterPass(new \hkod\frontmatter\YamlParser)
    ->addBodyPass(new \hkod\frontmatter\MustacheParser)
    ->addBodyPass(new \hkod\frontmatter\MarkdownParser)
    ->setBlockParser(new \hkod\frontmatter\BlockParser('***', '***'))
    ->buildParser();

$document = "***
key: {{variable}}
***
This is a **{{text}}** template
";

$context = ['variable' => 'value', 'text' => 'markdown'];

$result = $parser->parse($document, $context);

// value
echo $result->getFrontmatter()['key'];

// <p>This is a <strong>markdown</strong> template</p>
echo $result->getBody();