PHPackages                             corwatts/yii2-markdown-files - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. corwatts/yii2-markdown-files

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

corwatts/yii2-markdown-files
============================

A simple flat-file Markdown renderer

3.0.1(1y ago)33.5k1BSD-3-ClausePHP

Since May 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/CorWatts/yii2-markdown-files)[ Packagist](https://packagist.org/packages/corwatts/yii2-markdown-files)[ Docs](https://github.com/CorWatts/yii2-markdown-files)[ RSS](/packages/corwatts-yii2-markdown-files/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (7)Versions (9)Used By (0)

Yii2 Markdown Files
===================

[](#yii2-markdown-files)

[![Build Status](https://github.com/corwatts/yii2-markdown-files/actions/workflows/actions.yml/badge.svg)](https://github.com/corwatts/yii2-markdown-files/actions/workflows/actions.yml/badge.svg)[![codecov](https://camo.githubusercontent.com/8f211dbc8e2549ab9e9ec2124c032e4d4696d1f376c8a721b8990c68cc49b00f/68747470733a2f2f636f6465636f762e696f2f67682f436f7257617474732f796969322d6d61726b646f776e2d66696c65732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/CorWatts/yii2-markdown-files)

yii2-markdown-files provides a simple way to write updates, posts, or blocks of text in individual Markdown files with YAML frontmatter, render them on the fly and use the rendered HTML and frontmatter however you like.

Give updates on your Yii2 site or have a list of posts for a simple blog or news feed. Store your posts in version control with the rest of your code. **No database required!**

The idea for this extension was influenced by Jekyll, a static website generator written in Ruby.

Installation
------------

[](#installation)

Install via composer:

```
composer require 'corwatts/yii2-markdown-files'
```

Configuration
-------------

[](#configuration)

Enable the module by adding the snippet below to your main.php configuration file.

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

- `class`: is the namespaced class for this module
- `posts`: is a path pointing to the directory containing publishable markdown files. The path can contain Yii2 aliases.
- `drafts`: is a path pointing to the directory containing markdown files that aren't quite ready for publishing. The path can contain Yii2 aliases. **Drafts are only rendered in the Yii2 `dev` environment.**

**Note:** If you're going to use the included console command ensure this configuration is added somewhere the console application can access (like `common/config/main.php`).

Usage
-----

[](#usage)

Before rendering and displaying posts the individual post files must be created. A simple way to scaffold new posts is using the console command included in this extension. See below for instructions on how to set it up and use it.

It is easy to create new posts *without* the included console command. Posts follow a specific ruleset:

- Create a file in the `posts` or `drafts` directory path specified in the module configuration above.
- Similar to Jekyll, the filename has a specific format. It should start with the date (YYYY-MM-DD format) followed by a snake\_cased description, and ending with the `.md` extension. Something like `2017-05-20_test_post_1.md`. When these files are processed the date is extracted from the filename. The rest of the descriptive filename is not used at this time.

### Post Template

[](#post-template)

A basic post template looks like this:

```
---------
author: "Your Name"
title: "Blog Title"
---------

A post
```

The default YAML variables are `author` and `title`. You can set your own custom variables here too, and they will be available on the rendered side.

After running files through `parseFiles()` you'll have an array of arrays that look like this:

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

You can then iterate over the arrays and render the posts in your view partial.

```
$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
```

*Protip:* Cache the results to avoid having to recompile all your posts on every page hit.

Now you can render this data in a simple partial like this:

```
