3
votes

I'm rewriting my blog to use Jekyll. Jekyll uses the Liquid templating language so it makes it a little more difficult to learn how to customize.

I have a lot of .md files (markdown) one for each post. For every file I have in the following in the front matter:

---
layout: portfolio
title:  "Project Title"
date:   2015-12-12 17:53:00
categories: portfolio
tag: web
featured: true
---

In the tag section, I use one or more tags for each project. I know that with:

 {% for project in site.categories['project']%}
   do some stuff
 {% endfor %}

I iterate for every project. But I have the same tag for multiple files and I want to have a distinct list of tags. How can I achieve that?

2
Did you want to list projects by tag ? Not clear.David Jacquel
Exactly the above answer!pinguinone

2 Answers

4
votes

Updated solution from 2018:

<!-- Gather unique tags from articles -->
{% assign tags_articles =  site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Article tags: {{ tags_articles | join: "," | prepend: "[" | append: "]" }}</h2>

<!-- Gather unique tags from tutorials -->
{% assign tags_tutorials =  site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Tutorial tags: {{ tags_tutorials | join: "," | prepend: "[" | append: "]" }}</h2>

<!-- Combine and leave unique only -->
{% assign combo = tags_articles | concat: tags_tutorials | uniq | sort %}
<h2>Combo: {{ combo | join: "," | prepend: "[" | append: "]" }}</h2>
2
votes

I think you are looking for something like this:


<!-- Create empty arrays -->
{% assign tags = '' | split: ',' %}
{% assign unique_tags = '' | split: ',' %}

<!-- Map and flatten -->
{% assign article_tags =  site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' %}
{% assign tutorial_tags =  site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' %}

<!-- Push to tags -->
{% for tag in article_tags '%}
  {% assign tags = tags | push: tag %}
{% endfor %}
{% for tag in tutorial_tags '%}
  {% assign tags = tags | push: tag %}
{% endfor %}

<!-- Uniq -->
{% assign tags = tags | sort %}
{% for tag in tags %}

  <!-- If not equal to previous then it must be unique as sorted -->
  {% unless tag == previous %}

    <!-- Push to unique_tags -->
    {% assign unique_tags = unique_tags | push: tag %}
  {% endunless %}

  {% assign previous = tag %}
{% endfor %}

Then unique_tags should have the result you want, I think.