The Stochastic Game
Ramblings of General Geekery

Piecrust 0.7.0

The new version of PieCrust has been tagged in the stable branch yesterday, and oh boy does it have some cool features. You can read about them in the CHANGELOG, or keep on reading for a more detailed presentation.

Rhubarbed Strawberry Daiquiri Tart Plated

Blog archives

Until now, if you wanted to display a list of posts on your website, you had to go through the pagination.posts variable, which has the nasty side-effect of generating pagination for the current page (i.e. the page would have as many sub-pages as needed to show all your posts, with some expected links like “next entries” and “previous entries” to navigate between them). You could of course use the single_page setting to prevent any sub-pages from being created, but that was a bit awkward, especially since you also probably wanted to tweak other things, like the number of posts to show.

Now there’s a more straightforward way with the blog.posts variable, which returns all your posts without any limit or filtering. It also doesn’t affect pagination in any way. The nice thing is that you can still skip, limit or otherwise filter the posts.

For example, on my own home page I show the latest 7 posts. This can be done like this:

{% for post in blog.posts.limit(7) %}
* [{{ post.title }}]({{ post.url }})
{% endfor %}

You can also use Twig’s built-in slice filter to skip and limit the posts, as in:

{% for post in blog.posts|slice(0, 7) %}
* [{{ post.title }}]({{ post.url }})
{% endfor %}

But you can also do more advanced things, like:

    has_tags: cooking
        is_status: draft
        has_tags: appetizer
{% for post in blog.posts.filter('my_filter').limit(5) %}
* [{{ post.title }}]({{ post.url }})
{% endfor %}

The blog variable also contains other cool things, like tags and categories, and, because this whole feature is called “blog archives” after all, years and months. This makes it possible to do the following:

# Blog Archives

{% for month in blog.months %}
## {{ month.timestamp|date('M Y') }}

{% for post in %}
* [{{ post.title }}]({{ post.url }})
{% endfor %}
{% endfor %}

For more information about the blog archives, go read all about it on the official documentation.

Chained file processors

This is pretty cool: PieCrust can now chain your file processors when baking (and even previewing!) your website.

This means that if you have, say, the LessCSS and YUICompressor processors (the first one ships with PieCrust, the second one is available as a plugin), not only will your Less files be compiled to CSS, but they will also be further processed by the compressor, resulting in a compiled and compressed file!

There’s an example of how it works on the official documentation.

New pagination features

The pagination object now has two new properties: next_post and prev_post.

These objects will be valid if the current page is itself a post, and they will point to (unsurprisingly) the next and previous posts in the blog. Those are the full blown post objects, which means you have access to the title, timestamp, etc.

This is useful if you want a blog layout where you have links to those previous/next posts at the bottom of each article (like this very blog!).

New chef commands

Chef, the command line tool for PieCrust, got a few new commands of its own:

  • chef showconfig prints parts or all of the website’s configuration settings.

  • chef find lets you find pages, posts and templates in the current website.

  • chef plugins now has sub-commands to let you search and install plugins from a plugin source. Right now, the only type of plugin source that works is a BitBucket source (it will look for any repository that starts with PieCrust-Plugin-), and the only default source is my own BitBucket account (you can add more sources with the site/plugins_sources configuration setting).

New importers

You can now import posts and pages from a Jekyll or Joomla website.

Also, note that the chef import command changed:

  • The format and source options are now mandatory arguments – although if you use the old syntax you will be warned about the change.

  • You can now specify importer-specific options, like the Wordpress SQL table prefix, as a command-line option, which is a lot better than the previous awkward connection-string suffix hack.

Twig extensions

The built-in Twig extension now comes with the additional following features:

  • A striptag filter, which strips outer HTML tags from a string.

  • A textfrom function, which includes text from an arbitrary text file. This is useful if, like me, you write your blog posts as simple text files stored in Dropbox, but you sometimes want to preview the article in the context of your blog without having to copy/paste the text.

Also, you can set the twig/auto_escape site configuration setting to false if you don’t want Twig to auto-escape your stuff automatically. This means you won’t have to pass all your articles through the raw filter in your templates anymore, but it means you will have to make sure you escape other stuff accordingly.

Miscellaneous changes

  • Pages are now cached differently: they used to be parsed, formatted and rendered in one go, with the final result cached to make it super fast to re-use it templates. However, it broke some advanced use-cases with Twig and other template engines that support inclusion and such things.

Now, only the parsed pages are cached – formatting and rendering will happen every time the page is requestedduring a session. This actually doesn’t affect performance that much because, within a session (a preview request, or a baking), the final page is cached in memory anyway.

  • The bake information file (bakeinfo.json) used to pollute the output directory. Not anymore, as it’s now saved in the cache directory.

  • The Less and Markdown libraries have been updated to their latest version.

That’s it! Go grab PieCrust 0.7 from Github or BitBucket as ususal, and be sure to send me feedback and bug reports.