Accessing specific entry in a CSV file in _data folder


#1

I am unsure how to access data for a specific entry in a CSV file in my _data folder. Right now, my CSV looks like this:

SourceFile,FileName,ImageWidth,ImageHeight
images/A.jpg,A.jpg,2048,1024
images/B.jpg,B.jpg,800,599

I’d like to extract the corresponding ImageWidth and ImageHeight data for images I post. Something along these lines:

{% assign image = site.data.image_dimensions[FileName] %}

The data used in the examples in the Jekyll documentation page is formatted differently, and I am having a hard time figuring out how to go about this. I don’t have much flexibility in terms of how the data in the CSV is formatted, as it is being created by exiftool. Any guidance would be greatly appreciated.


#2

ProTip™: Look at Jekyll’s feature tests if you look for examples

Use Jekyll’s inspect filter in your templates when developing to see how objects are formatted, it might help you know how to access them:

{{ site.data.images | inspect }}

For example, you could write something like the following to display all images stored in your data file:

path,filename,width,height
/assets/images,jekyll-search.png,515,446
/assets/images,jamstack-architecture.png,974,463
<ul>
  {% for image in site.data.images %}
    <li>
      <img src="{{ image.path | relative_url }}/{{ image.filename }}" width="{{ image.width }}" height="{{ image.height }}" />
    </li>
  {% endfor %}
</ul>

You can access a specific entry either by user a where filter;

{{ site.data.images | where:"filename","jekyll-search.png" }}

or by using its index: {{ site.data.images[0].filename }}