2016-10-12 29 views
1

要設置場景,我的作者信息存儲在我的Jekyll _data文件夾內的directory.json中。比較來自不同Jekyll_data文件的數據

"name": "Brandon Bellew", 
    "bio": "Brandon Bellew is a great writer.", 
    "email": "[email protected]", 
    "specialties": "introspection, ventilation systems, memorization, post-prandial revelry", 
    "photo": "http://www.tpgweb2.net/headshots/thumbs/brandon-b-thumb-2.jpg" 

我也存儲在catalogue.json文章,也是我的化身內_data文件夾

"name": "Beginner's Guide to Google Analytics", 
    "author": "Brandon Bellew", 
    "date": "May 18 2016", 
    "description": "The Google Analytics (GA) dashboard is quite intuitive, and it's easy to collect basic information (number of users on the site, average time spent on site, etc.) without having a deep background in analytics. But what about specifics?", 
    "category": "Analytics" 

我通過我的catalogue.json文件,並顯示所有的文章試圖週期中文章信息,但從我的directory.json文件中的正確作者拉照片。

實質上,我試圖做的是:如果目錄中文章的「作者」與目錄項中作者的「名稱」匹配,則顯示與該作者相關的照片。這樣我就不必爲他們寫的每篇文章都將作者的照片另存爲一個單獨的項目。

這是我在目錄從正確的作者拉動照片失敗的嘗試:

<h4>Articles</h4> 
    {% for item in site.data.catalogue %} 
     {% for item in site.data.directory %} 
     {% if item.name == page.author %} 
     <img src="{{ item.photo }}" > 
     {% endif %} 
     {% endfor %} 
    {% endfor %} 

使用傑基爾這是可能的,或者是有不同的方式,我需要構建呢?

回答

1

我相信去掉重複的item參考將解決當前的代碼:

{% for catalogue in site.data.catalogue %} 
    {% for item in site.data.directory %} 
    {% if item.name == catalogue.author %} 
     <img src="{{ item.photo }}" > 
    {% endif %} 
    {% endfor %} 
{% endfor %} 

你可以組織你的作家目錄,這樣就可以不必遍歷每個作者訪問的附加信息。

authors.json

{ 
    "Fred Flintstone": { 
     "bio": "Doesn't wear shoes" 
     "image": "/images/fred.png" 
    } 
} 

然後,你可以訪問圖像,像這樣(你可以改善這種檢查是否存在的作者):

{% for catalogue in site.data.catalogue %} 
    <img src="{{ site.data.authors[catalogue.author].photo }}" > 
{% endfor %} 
0

您可以使用where過濾器像這樣:

{% for article in site.data.catalogue %} 
    {% assign author = site.data.directory | where: 'name', article.author | first %} 
    {% if author %}<img src="{{ author.photo }}" >{% endif %} 
    {% endfor %} 
{% endfor %} 
相關問題