PHP code example of corwatts / yii2-markdown-files

1. Go to this page and download the library: Download corwatts/yii2-markdown-files 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/ */

    

corwatts / yii2-markdown-files example snippets


'modules' => [
  'blog' => [ // name this module what you like
    'class' => \corwatts\MarkdownFiles\Module::className(),
    'posts' => '@frontend/views/blog/posts',
    'drafts' => '@frontend/views/blog/drafts',
  ]
],

[
  [
    'date'   => [
      'year'   => '2017',
      'month'  => '05',
      'day'    => '12',
      'full'   => '2017-05-12',
      'name'   => 'hello_world'
    ],
    'yaml'   => [
      'title'  => 'Blog Title',
      'author' => 'Your Name'
    ],
    'content' => '<p>A post</p>'
  ],
  ...[other posts]...
]

$posts = \Yii::$app->getModule('blog'); // get module instance
                   ->fetch()    // get a list of markdown files
                   ->parse()    // parse the list of files
                   ->result;    // grab the array of parsed files
return $this->render('index', ['posts'=>$posts]); //render the view


use \yii\helpers\Html;

foreach($posts as $file) {
  $yaml    = $file['yaml'];
  $content = $file['content'];

  $date = Html::encode(date('F j, Y', strtotime($file['date']['full'])));
  print "<h4>".Html::encode($yaml['title'])."</h4>";
  print "<em>Written by ".Html::encode($yaml['author'])." on $date</em>";
  print $content;
}

'bootstrap' => ['blog'],