PHP code example of kzykhys / yaml-front-matter

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

    

kzykhys / yaml-front-matter example snippets

 php


ys\FrontMatter\FrontMatter;

// Parse a document
$document = FrontMatter::parse(<<<EOF
---
title: Hello World
category: blog
layout: post
tags:
    - technology
    - PHP
vars:
    pingback_url: http://example.com/pingback?id={{post.id}}
    description: ~
extra:
    markdown:
        gfm: true
        textile: true
    template: twig
---

{% block content %}
    Lorem Ipsum ...
{% endblock %}
EOF
);

var_dump($document); // An instance of `Document`
var_dump($document->getConfig()); // An array {title:"Hello World", category: "blog"....}
var_dump($document->getContent()); // "{% block content %}...."
var_dump((string) $document); // "{% block content %}...."
var_dump($document["layout"]); // "post"
var_dump($document["extra"]["template"]); // "twig"

foreach ($document as $key => $value) {
    var_dump($key, $value); // "title", "Hello World"
    break;
}
 php


use KzykHys\FrontMatter\Document;
use KzykHys\FrontMatter\FrontMatter;

ray('uncategorized', 'test');
$document->setContent('<p>Hello World!</p>');

echo FrontMatter::dump($document);
 php
$parent = FrontMatter::parse(<<<EOF
---
extra:
    template: twig
category: blog
tags:
    - PHP
---

Lorem Ipsum ...
EOF
);

$document = FrontMatter::parse(<<<EOF
---
extra:
    template: smarty
tags:
    - HHVM
    - PHP5.5
---

Lorem Ipsum ...
EOF
);

$document->inherit($parent);
var_dump($document->getConfig());

array(2) {
  'extra' => array(1) {
    'template' => string(6) "smarty"
  }
  'category' => string(4) "blog"
  'tags' => array(3) {
    [0] => string(3) "PHP"
    [1] => string(4) "HHVM"
    [2] => string(6) "PHP5.5"
  }
}