2016-04-30 23 views
1

我正在尝试制作按标签分组的集合中的页面列表。我知道这是很容易可以与哲基尔的内置site.tags功能,我可以(有)的东西,如实现:按标签列出Jekyll Collection页面

<h3>Tags</h3> 

{% for tag in site.tags %} 
    {% assign t = tag | first %} 
    {% assign posts = tag | last %} 

<h4><a name="{{t | downcase | replace:" ","-" }}"></a><a class="internal" href="/tagged/#{{t | downcase | replace:" ","-" }}">{{ t | downcase }}</a></h4> 

<ul> 
{% for post in posts %} 
    {% if post.tags contains t %} 
    <li> 
    <a href="{{ post.url }}">{{ post.title }}</a> 
    <span class="date">{{ post.date | date: "%B %-d, %Y" }}</span> 
    </li> 
    {% endif %} 
{% endfor %} 
</ul> 

<hr> 

{% endfor %} 

为了得到这样的:

Grouped by tags

我想在名为note的集合中复制site.tags函数。使用YAML标题中的tags: [urban growth, California, industrialization],我的收藏中的标记与帖子的标记相同。但我想用集合来代替它。我几乎可以达到什么样的我想有以下:

{% assign collection = site.note | group_by: "tags" | uniq %} 
{% for group in collection %} 
{% comment %} This is super hacky and I don't like it {% endcomment%} 
    <h3>{{ group.name | replace: '"', '' | replace: '[', '' | replace: ']', '' }}</h3> 
    <ul> 
    {% for item in group.items %} 
    <li><a href="{{ item.url | prepend: site.baseurl | prepend: site.url }}">{{ item.title }}</a></li> 
    {% endfor %} 
    </ul> 
{%endfor%} 

但你很可能会看到,这不破标签伸到其独特的群体;相反,YAML中的每组标签都被视为单个大标签。任何构建唯一标签数组并在其下列出Collection页面的指针?

回答

10

试试这个:

{% assign tags = site.note | map: 'tags' | join: ',' | split: ',' | uniq %} 
{% for tag in tags %} 
    <h3>{{ tag }}</h3> 
    <ul> 
    {% for note in site.note %} 
    {% if note.tags contains tag %} 
    <li><a href="{{ site.baseurl }}{{ note.url }}">{{ note.title }}</a></li> 
    {% endif %} 
    {% endfor %} 
    </ul> 
{% endfor %} 
+0

这伟大的工作,谢谢! –

1

的大卫Jacquel的答复中提到tags分配方法可以简化为:

{% assign tags = site.note | map: 'tags' | uniq %}