2016-11-28 61 views
0

标准显示JSON数据我有以下结构的JSON文件:具有杰基尔

{ 
    "resources": [ 
     { 
      "date": "Nov 7", 
      "content": [ 
       { 
        "articleTitle":"The Distribution of Users’ Computer Skills: Worse Than You Think", 
        "articleUrl":"https://www.nngroup.com/articles/computer-skill-levels/?ref=boomkrak", 
        "articleAuthor": "Jakob Nielson", 
        "articleTag": "ux, web design" 
       }, 
       { 
        "articleTitle":"How to Use Animation to Improve UX", 
        "articleUrl":"https://uxplanet.org/how-to-use-animation-to-improve-ux-338819e93bdb#.84b3m022s?ref=boomkrak", 
        "articleAuthor": "Nick Babich", 
        "articleTag": "ux, animation" 
       } 
      ] 
     }, 
     { 
      "date": "Nov 15", 
      "content": [ 
       { 
        "articleTitle":" 7 Things Every Designer Needs to Know about Accessibility", 
        "articleUrl":"https://medium.com/salesforce-ux/7-things-every-designer-needs-to-know-about-accessibility-64f105f0881b#.5pgg5014x?ref=boomkrak", 
        "articleAuthor": "Jesse Hausler", 
        "articleTag": "ux, web design, accessibility" 
       }, 
       { 
        "articleTitle":"Get the most out of your research with storytelling", 
        "articleUrl":"https://blog.intercom.com/get-the-most-out-of-your-research-storytelling/?ref=boomkrak", 
        "articleAuthor": "Jillian Wells", 
        "articleTag": "design research, collaboration" 
       } 
      ] 
     } 
    ] 
} 

我想表明基于每个标签的文章。例如,如果我想显示ux文章,则应显示带有ux标记的所有文章。

任何人都知道如何在Jekyll中做到这一点?

回答

1

考虑到你的数据都是在_datas/articles.json,你可以使用这个包含文件(_includes/listbyTag.html):

{% assign tag = include.tag %} 
{% assign days = site.data.articles.resources %} 
{% assign list = ""| split: "/" %} {% comment %} creates an empty array {% endcomment %} 

{% for day in days %} 
    {% for article in day.content %} 
    {% if article.articleTag contains tag %} 
     {% assign list = list | push: article %} 
    {% endif %} 
    {% endfor %} 
{% endfor %} 

{% if list.size != 0 %} 
    <h3>Found {{ list.size }} posts for tag : {{ tag }}</h3> 
    <ul> 
    {% for post in list %} 
     <li>{{ post.articleTitle }}</li> 
    {% endfor %} 
    </ul> 
{% endif %} 

现在你可以在任何地方包括它:

{% include listByTag.html tag="ux" %} 

或为一个标签页面:

--- 
tag: ux 
title: my title 
layout: default 
--- 
{% include listByTag.html tag=page.tag %} 
+0

如何在链接上传递变量标签,使其可以动态变化?也许就像在PHP上发送GET/POST方法? –

+0

Jekyll是静态的,所以没有服务器端的逻辑。您将需要通过标记创建一个页面。看到我编辑的答案。 –

+0

啊我明白了。谢谢大卫! –