From 8b66ea9bea3ac81a34222d56fd073f496515380e Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Wed, 19 Sep 2018 16:52:49 +0200 Subject: [PATCH] Add archives page Blogs commonly only display the latest `n` blog posts on the home page, while showing the full list on a separate archive page. This change implements that. --- .eleventy.js | 5 +++++ archive.njk | 12 ++++++++++++ index.njk | 7 ++++++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 archive.njk diff --git a/.eleventy.js b/.eleventy.js index b387b3c..48a2aab 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -12,6 +12,11 @@ module.exports = function(eleventyConfig) { return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy"); }); + // Get the first `n` elements of a collection. + eleventyConfig.addFilter("head", (array, n) => { + return array.slice(0, n); + }); + // only content in the `posts/` directory eleventyConfig.addCollection("posts", function(collection) { return collection.getAllSorted().filter(function(item) { diff --git a/archive.njk b/archive.njk new file mode 100644 index 0000000..235fd75 --- /dev/null +++ b/archive.njk @@ -0,0 +1,12 @@ +--- +layout: layouts/home.njk +tags: + - nav +navtitle: Archive +permalink: posts/index.html +--- + +

Blog post archive

+ +{% set postslist = collections.posts %} +{% include "postslist.njk" %} diff --git a/index.njk b/index.njk index 9a30ce5..db6baea 100644 --- a/index.njk +++ b/index.njk @@ -4,5 +4,10 @@ tags: - nav navtitle: Home --- -{% set postslist = collections.posts %} + +

Latest 2 blog posts

+ +{% set postslist = collections.posts | head(2) %} {% include "postslist.njk" %} + +

More posts can be found in the blog archive.