Jekyll中不同的标签列表

时间:2015-12-12 17:21:00

标签: jekyll liquid

我正在重写我的博客以使用Jekyll。 Jekyll使用Liquid模板语言,因此学习如何定制更加困难。

我为每个帖子提供了很多.md个文件(降价)。对于我在前面的内容中的每个文件:

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

在标签部分,我为每个项目使用一个或多个标签。我知道:

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

我为每个项目迭代。但我有多个文件的相同标签,我想有一个不同的标签列表。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

我认为您正在寻找类似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 %}

然后,unique_tags应该有你想要的结果,我想。

答案 1 :(得分:1)

2018年更新的解决方案:

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

<!-- Gather unique tags from tutorials -->
{% assign tags_tutorials =  site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Tutorial tags: {{ recipes_tags | 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>