Scheduling Posts in Jigsaw

Mon 1st June 2020 By David T. Sadler.

Jigsaw is a static site generator that I use for my site and one of its features is the ability to create a filter that determines which items in a collection will be included in the final build.

It works by you adding a filter key to a collection's array in your config.php file, and specifying a callable that accepts an item from the collection and that returns a boolean. By returning false an item will not be built.

Below is how I have setup my config.production.php file which is used when building the site for deployment.

<?php

use Carbon\Carbon;

return [
    'collections' => [
        'posts' => [
            'filter' => function ($item) {
                $date = $item->date ? Carbon::createFromFormat('U', $item->date) : null;
                // Only publish posts that have a date and which is not in the future.
                return $date ? $date <= Carbon::now() : false;
            }
        ],
    ],
];

When deploying the site each post is passed to this filter. The first thing it does is convert the date that has been specified in the post's YAML front matter into a Carbon instance. It then returns true if the date is on or before the current date, I.e. when the site is been deployed.

With this filter I can specify future dates for several posts and they will only published once that date comes around. Posts are also exluded if a date has not been specified. This allows me to have posts that are a work in progress and shouldn't be published.

Links

Jigsaw - Static Site Generator for PHP Developers.Using Filters in Jigsaw.Carbon - PHP API Extension for DateTime.Jigsaw - Read More Posts.

I don't have comments as I don't want to manage them. You can however contact me at the below address if you want to.

Email david@davidtsadler.com

License

The contents of this site is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Copyright © 2021 David T. Sadler.

Return to Homepage.