2015-06-21 20 views
3

在Jekyll 2.5.3中,我使用albumscollection(因爲我不僅需要存儲數據,還需要生成頁面)。按名稱獲取Jekyll收藏品的優雅方式?

當傑奇Data Files使用,你可以得到一樣簡單特定項目的數據:site.data.albums[foo]

但隨着集合,事情變得更糟。所有這些我已經試過的方法不需要做任何事情:

  • site.albums[foo]
  • site.collections.albums[foo]
  • site.collections.albums.docs[foo]
  • site.collections.albums.files[foo]

所以我需要:

  1. 遍歷所有收集項目小號
  2. 對於他們每個人get a bare name
  3. 的一些目標名稱
  4. 如果名稱匹配比較這個名稱,最後指定集合項數據的一些變量使用

什麼更好的建議?

回答

2

我今天剛剛做完了這件事,你對你的斷言是正確的。以下是我做的:

<!-- this is in a partial with a 'name' parameter --> 
{% for item in site.albums %} 
    {% assign name = item.path | split:"/" | last | split:"." | first %} 
    {% if name == include.name %} 
     {% assign collectionItem = item %} 
    {% endif %} 
{% endfor %} 

使用

{{ collectionItem.title }} 
{{ collectionItem.url }} 
{{ collectionItem.path }} 

它甚至可以被用來填充值等諧音,象這樣:

{% include card.html title=workItem.title bg=workItem.card.bg href=workItem.url %} 
+0

這是要殺死構建時間。太糟糕了,沒有更好的辦法。 –

2

As of Jekyll 3.2,您可以使用過濾器where_exp可通過其任何屬性過濾集合。所以,如果我有一個albums收集以下項目:

--- 
title: My Amazing Album 
--- 
... 

我能搶到第一場比賽由該名稱的項目:

{% assign album = site.albums 
    | where_exp:"album", "album.title == 'My Amazing Album'" 
    | first %} 

請注意,我用的是first濾波器,因爲where_exp返回匹配項目數組。

,然後用它什麼辦法,我想:我不能保證這種方法的構建性能

<h1>{{ album.title }}</h1> 

,但我想象它比液體循環更好。