New in Symplify 3: Statie Generators

This post was updated on September 2018
Updated with Statie 5.0, NEON → YAML, Twig and statie.yml config.

Statie missed one important feature. Posts were the only elements that you could render as standlone page. But what if you want a web porfolio, not with posts but with features projects? Or lectures pages?

Statie 3 to the rescue!

Coupled Approach in Statie 2

Posts in Statie 2 were enabled by defaults with following hard coded logic:

You could change a post_route or layout in specific post file, but that was it. No flexibility, no extendability and modification of PostFile class was not possible.

In Pehapkari.cz we're staring community lectures and we need not only posts, but also lectures to be on stand-alone page, e.g.

If this could be only configurable, it would benefit many websites that are more than just a list of posts.

Configurable Approach in Statie 3 with Generators

So here comes Statie 3 Generators. Same posts approach is now set by default, but also configurable in parameters > generators section:

parameters:
    generators:
        posts: # key useful for exception reports
            # name of variable inside single such item
            variable: post
            # name of variable that contains all items
            varbiale_global: posts
            # directory, where to look for them
            path: '_posts'
            # which layout to use, will be converted to "_layout/post.twig"
            layout: 'post'
            # and url prefix, e.g. /blog/some-post.md
            route_prefix: 'blog'
            # an object that will wrap it's logic, you can add helper methods into it and use it in templates
            object: 'Symplify\Statie\Renderable\File\PostFile'

The whole old posts logic is now configurable.

Do you need to change path to _articles?

parameters:
    generators:
        posts:
            path: '_articles'

Or do you want to use own simpler object?

parameters:
    generators:
        posts:
            object: 'MyWebsite\Statie\SimplePostFile'

How to Add Own Generator?

So how did we solved lectures in Pehapkari.cz website?

Adding new Generator Element is now easy:

# statie.yml
parameters:
    generators:
        lectures:
            variable: 'lecture'
            variable_global: 'lectures'
            path: '_lectures'
            layout: 'lecture'
            route_prefix: 'kurz'
            object: 'Pehapkari\Website\Statie\Generator\LectureFile'

Do you want real code? See this commit that has it all.

The configuration is as simple as possible, so object is optional. You can read how to set own file in documentation.

How to add new Lecture?

  1. Create new *.md file in source/_lectures/ directory
  2. Have _layout/lecture.twig ready
  3. Create LectureFile with extra getTitle() method

That's all!

These lectures will be available in every template under {$lectures} variable, as configured in variable_global option.

So you can create page source/all-lectures.twig with all lectures (ordered by filename):

{% for lecture in lectures %}
    <h2>{{ lecture.getTitle() }}</h2>
{% endfor %}

Try Generators Out

Just upgrade Statie:

composer update symplify/statie

and add lectures, talks with details, docs... anything you need.

Date Me!

One more surprise to prevent. In case you use route_prefix with date in it:

# statie.yaml
parameters:
    generators:
        posts:
            route_prefix: 'blog/:year/:month/:day'

remember, the date has to be in start of the filename:

_lectures/2018-01-30-use-open-source-statie-for-open-blogging.md

When setup properly, you get this file in generated code:

2018/01/30/use-open-source-statie-for-open-blogging

How to Upgrade from Statie 2 to Statie 3?

"Real code is worth of thousand words describing it."
Anonymous Lazy Programmer

See these PRs:


Happy Generating!


  Continue Learning


Typo? Fix it, please  and join 29 people who build this website

GitHub RSS @votrubaT Runs on Statie Hosted on GitHub Build by 30 people

Like what I write about? Hire me & we can work together