【问题标题】:Distinct list of tags in JekyllJekyll 中不同的标签列表
【发布时间】:2016-03-18 11:59:57
【问题描述】:

我正在重写我的博客以使用 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 %}

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

【问题讨论】:

  • 您想按标签列出项目吗?不清楚。
  • 正是上面的答案!

标签: jekyll liquid


【解决方案1】:

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>

【讨论】:

  • | join: ',' | join: ','是什么意思?
  • @DavideIcardi 我现在不记得 jekyll 模板语法,但现在看它我猜是 | join: ',' | split: ',' 操作序列导致了一些不错的副作用。空白区域被修剪?或者可能由 split 返回的类型被 uniq 接受?一定是这样的。
【解决方案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 应该有你想要的结果。

【讨论】:

  • 我要寻找的东西太棒了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2014-06-24
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
相关资源
最近更新 更多