Use Phrozn as a blog

04 Sep 2013

Phrozn is static site generator written, similar to Jekyll, but written in PHP.

The major weekness is that we can’t list all entries (for example, to create an index or a feed).

I wrote a little plugin to fix this:

<?php

namespace PhroznPlugin\Provider;

class Entries
    extends \Phrozn\Provider\Base
    implements \Phrozn\Provider
{

    public function get()
    {
        $base = __DIR__ . '/../../../entries/posts';
        $result = array();
        foreach (array_reverse(glob($base . '*')) as $file) {
            $factory = new \Phrozn\Site\View\Factory($file);
            $view = $factory->create();

            $entry = $view->getParams('page');
            if (!empty($entry['skip'])) continue;

            $entry['permalink'] = $view->getOutputFile();

            $view->hasLayout(false);
            $entry['content'] = $view->render();
            list($entry['summary']) = explode("\n", strip_tags($entry['content']));

            $result[] = $entry;
        }

        usort($result, function($a, $b) {
                if ($a['date'] == $b['date']) return 0;
                return $a['date'] < $b['date'] ? 1 : -1;
        });

        return $result;
    }
}
providers:
    entries:
        provider: Entries
---
(% for entry in this.providers.entries %)
<article>
    (( entry.summary ))
    <a href="(( entry.permalink ))">Continue reading</a>
</article>
(% endfor %)
providers:
    entries:
        provider: Entries
layout: feed.twig
permalink: feed.xml
---
(% for entry in this.providers.entries %)
<item>
    <title>(( entry.title ))</title>
    <link>(( entry.permalink ))</link>
    <description><![CDATA[(( entry.content ))]]></description>
    <pubDate>(( entry.date|date('r') ))</pubDate>
    <author>(( entry.author ))</author>
    <guid>(( entry.permalink ))</guid>
</item>
(% endfor %)

See in action: http://blog.alteroot.org