3
votes

I need help using _data in Jekyll to generate content.

Let's say I want to create a directory displaying countries that have blocked Twitter and Facebook. I understand how to query the _data folder but how do I create something like categories in the _data .yml and query that data?

Let me try to explain.

Twitter is blocked in Turkey & Iran so I'd start with this (networks.yml in _data):

- network: Twitter

This is where I get stuck. What I don't understand is if I'd want to "tag" or "categorize" Twitter like this:

- network: Twitter
  blockedcountries: [Turkey, Iran, Iraq]

- network: Facebook
  blockedcountries: [Iraq, Turkey]

Then I'd want pages at mysite.com/turkey/ that would display the networks tagged with turkey. Something like this:

{% for network in site.data.networks %}
{% if network.blockedcountries == Turkey %}
        <li>{{ network.network }} is blocked in Turkey</li>
{% endif %}
{% endfor %}`

Which would display: 
 - Twitter is blocked in Turkey
 - Facebook is blocked in Turkey

Appreciate any help and will tip Bitcoin if solved!

1

1 Answers

3
votes

Your YAML is right. The Problem seems to be in your if statement:

{% if network.blockedcountries == Turkey %}

network.blockedcountries is an array(a list) and therefore your if statement needs to be like this:

{% if network.blockedcountries contains "Turkey" %}
  <li>{{ network.network }} is blocked in Turkey</li>
{% endif %}

Jekyll is using the Liquid markup language for its templates. You can read more about its possibilities here. Maybe the liquid case statement is also helpful for further optimization.


Here is my "full" solution:

My data in _data/networks.yml

- name: Twitter
  blockedcountries: [Turkey, Iraq, Iran]
- name: Facebook
  blockedcountries: [Turkey, Iraq]

My liquid template in index.html

<ul>
  {% for network in site.data.networks %}
    {% if network.blockedcountries contains "Turkey" %}
      <li>{{ network.name }} is blocked in Turkey!</li>
    {% endif %}
  {% endfor %}
</ul>