The Home of David T. Sadler - All Posts About Jigsaw https://davidtsadler.com/posts/jigsaw/atom.xml 2020-06-01T12:00:00Z Scheduling Posts in Jigsaw https://davidtsadler.com/posts/jigsaw/2020-06-01/scheduling-posts-in-jigsaw/ David T. Sadler. 2020-06-01T12:00:00Z 2020-06-01T12:00:00Z <h1>Scheduling Posts in Jigsaw</h1><blockquote>Mon 1st June 2020 By David T. Sadler.</blockquote><p>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.</p><p>It works by you adding a filter key to a collection&#039;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.</p><p>Below is how I have setup my config.production.php file which is used when building the site for deployment.</p><pre><code class="php">&lt;?php use Carbon\Carbon; return [ &#039;collections&#039; =&gt; [ &#039;posts&#039; =&gt; [ &#039;filter&#039; =&gt; function ($item) { $date = $item-&gt;date ? Carbon::createFromFormat(&#039;U&#039;, $item-&gt;date) : null; // Only publish posts that have a date and which is not in the future. return $date ? $date &lt;= Carbon::now() : false; } ], ], ];</code></pre><p>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&#039;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. </p><p>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&#039;t be published.</p><h3>Links</h3><a href="https://jigsaw.tighten.co/">Jigsaw - Static Site Generator for PHP Developers.</a><a href="https://jigsaw.tighten.co/docs/collections-filtering/">Using Filters in Jigsaw.</a><a href="https://carbon.nesbot.com/">Carbon - PHP API Extension for DateTime.</a><a href="/posts/jigsaw/">Jigsaw - Read More Posts.</a><p>I don&#039;t have comments as I don&#039;t want to manage them. You can however contact me at the below address if you want to.</p><a href="mailto:david@davidtsadler.com">Email david@davidtsadler.com</a><h3>License</h3><a href="https://creativecommons.org/licenses/by-sa/4.0/">The contents of this site is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</a><p>Copyright © 2021 David T. Sadler.</p><a href="/">Return to Homepage.</a>