2016-04-30 50 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 %}