Skip to content

Instantly share code, notes, and snippets.

@DanRader
Last active December 6, 2018 01:09
Show Gist options
  • Select an option

  • Save DanRader/f19e6118b995a448791bce50e42d8f4f to your computer and use it in GitHub Desktop.

Select an option

Save DanRader/f19e6118b995a448791bce50e42d8f4f to your computer and use it in GitHub Desktop.
Jekyll—Color code categories controlled by front matter & a 'categories' collection.
---
title: Foo
date: 2016-12-17 11:09:00 -05:00
color: "#A96BE3"
---
<ul class= "post-cat-list">
<!-- Loop through the categories selected by the editor in the post -->
{% for post_cat in post.categories %}
<!-- assign a variable that captures the title of each category and makes it lowercase -->
{% assign post_cat-title = post_cat | downcase %}
<!-- Loop through all categories that you've made in your 'categories' collection -->
{% for site_cat in site.categories %}
<!-- assign a variable that capture the color from each category on your site -->
{% assign color = site_cat.color %}
<!-- assign a variable that captures the title of each category on your site and makes it lowercase -->
{% assign site_cat-title = site_cat.title | downcase %}
<!-- compare the category titles from your site vs the post. If they match, display the content -->
{% if site_cat-title == post_cat-title %}
<li>
<!-- use the color assigned in the category as a background color -->
<div class="box" style="background-color: {{ color | default 'blue'}}"></div>
<p>
<!-- Display the category title -->
{{ post_cat-title }}
</p>
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
title categories
title
foo
bar

{{ content }}

@DanRader
Copy link
Author

Here is my output

screen shot 2016-12-19 at 10 44 20 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment