I am using Jekyll [2.5.3] to build a prototype and need to access a specific rows from a file in _data
in my layouts.
In file _data/badges.csv
I have:
name, image_url
Heat, http://pathtoimage.com/image-1.png
Water, http://pathtoimage.com/image-2.png
Zoning, http://pathtoimage.com/image-3.png
...
In _layout/page.html
I am trying to access the Zoning
row from badges.csv
by passing a value to site.data
like so:
{% assign badge = site.data.badges.name["Zoning"] %}
{{ badge.image_url }}
Eventually I will make ["Zoning"]
a page variable to load specific badges based on the page.
I've tried [Zoning]
, ['Zoning']
, ["Zoning"]
. None of these output any data and do not throw an error during jekyll build
.
How can I access a specific row from the CSV?
I believe CSV files are treated as arrays instead of maps. If you want to find a specific entry in a CSV you could do something like this:
{% for badge in site.data.badges %}
{% if badge.name == "Zoning" %}
{{ badge.image_url }}
{% endif %}
{% endfor %}
You could then move that into _includes
and pass an argument to reuse the functionality:
_includes/badge-url.html
{% for badge in site.data.badges %}
{% if badge.name == include.name %}
{{ badge.image_url }}
{% endif %}
{% endfor %}
Including Zoning
{% include badge-url.html name="Zoning" %}