From 239c94929851fac56f6bc5a5b12fa8568c8e69ef Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Fri, 4 Mar 2016 21:45:30 -0500 Subject: [PATCH] Add "group by array" include to simplify archive pages --- _includes/group-by-array.html | 47 +++++++++++++++++++++++++++++++++++ _pages/category-archive.html | 31 ++++++++++------------- _pages/recipes-archive.html | 22 ++++++++++++++++ _pages/tag-archive.html | 32 +++++++++++------------- 4 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 _includes/group-by-array.html create mode 100644 _pages/recipes-archive.html diff --git a/_includes/group-by-array.html b/_includes/group-by-array.html new file mode 100644 index 00000000..33af7555 --- /dev/null +++ b/_includes/group-by-array.html @@ -0,0 +1,47 @@ + + + +{% assign __empty_array = '' | split: ',' %} +{% assign group_names = __empty_array %} +{% assign group_items = __empty_array %} + + +{% assign __names = include.collection | map: include.field %} + + +{% assign __names = __names | join: ',' | join: ',' | split: ',' %} + + +{% assign __names = __names | sort %} +{% for name in __names | sort %} + + + {% unless name == previous %} + + + {% assign group_names = group_names | push: name %} + {% endunless %} + + {% assign previous = name %} +{% endfor %} + + + +{% for name in group_names %} + + + {% assign __item = __empty_array %} + {% for __element in include.collection %} + {% if __element[include.field] contains name %} + {% assign __item = __item | push: __element %} + {% endif %} + {% endfor %} + + + {% assign group_items = group_items | push: __item %} +{% endfor %} diff --git a/_pages/category-archive.html b/_pages/category-archive.html index 617eef2c..6e6f5126 100644 --- a/_pages/category-archive.html +++ b/_pages/category-archive.html @@ -4,24 +4,19 @@ permalink: /category-archive/ title: "Posts by Category" --- {% include absolute-url.liquid %} -{% capture site_categories %}{% for category in site.categories %}{{ category | first }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %} -{% assign category_list = site_categories | split:',' | sort %} +{% include group-by-array.html collection=site.posts field='categories' %} -{% for item in (0..site.categories.size) %} - {% unless forloop.last %} - {% capture category_name %}{{ category_list[item] | strip_newlines }}{% endcapture %} -

{{ category_name }}

- {% for post in site.categories[category_name] %} - {% if post.title != null %} -
- {% if post.link %} -

{{ post.title }}

- {% else %} -

{{ post.title }}

-

{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}

- {% endif %} -
+{% for category in group_names %} + {% assign posts = group_items[forloop.index0] %} +

{{ category }}

+ {% for post in posts %} +
+ {% if post.link %} +

{{ post.title }}

+ {% else %} +

{{ post.title }}

+

{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}

{% endif %} - {% endfor %} - {% endunless %} +
+ {% endfor %} {% endfor %} \ No newline at end of file diff --git a/_pages/recipes-archive.html b/_pages/recipes-archive.html new file mode 100644 index 00000000..f059414a --- /dev/null +++ b/_pages/recipes-archive.html @@ -0,0 +1,22 @@ +--- +layout: archive +title: "Recipes Archive" +permalink: /recipes-archive/ +--- +{% include absolute-url.liquid %} +{% include group-by-array.html collection=site.recipes field='categories' %} + +{% for category in group_names %} + {% assign posts = group_items[forloop.index0] %} +

{{ category }}

+ {% for post in posts %} +
+ {% if post.link %} +

{{ post.title }}

+ {% else %} +

{{ post.title }}

+

{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}

+ {% endif %} +
+ {% endfor %} +{% endfor %} \ No newline at end of file diff --git a/_pages/tag-archive.html b/_pages/tag-archive.html index 66f007ab..503904ef 100644 --- a/_pages/tag-archive.html +++ b/_pages/tag-archive.html @@ -4,23 +4,19 @@ permalink: /tag-archive/ title: "Posts by Tags" --- {% include absolute-url.liquid %} -{% capture site_tags %}{% for tag in site.tags %}{{ tag | first }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %} -{% assign tag_list = site_tags | split:',' | sort %} +{% include group-by-array.html collection=site.posts field='tags' %} -{% for item in (0..site.tags.size) %} - {% unless forloop.last %} - {% capture tag_name %}{{ tag_list[item] | strip_newlines }}{% endcapture %} -

{{ tag_name }}

- {% for post in site.tags[tag_name] %}{% if post.title != null %} -
- {% if post.link %} -

{{ post.title }}

- {% else %} -

{{ post.title }}

-

{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}

- {% endif %} -
- {% endif %} - {% endfor %} - {% endunless %} +{% for tag in group_names %} + {% assign posts = group_items[forloop.index0] %} +

{{ tag }}

+ {% for post in posts %} +
+ {% if post.link %} +

{{ post.title }}

+ {% else %} +

{{ post.title }}

+

{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}

+ {% endif %} +
+ {% endfor %} {% endfor %} \ No newline at end of file