WORLD
April 18, 2018, 6:51pm
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.
Frank
April 23, 2018, 10:18pm
2
ProTip™: Look at Jekyll’s feature tests if you look for examples
Scenario: autoload *.csv files in _data directory
Given I have a _data directory
And I have a "_data/members.csv" file with content:
"""
name,age
Jack,28
Leon,34
"""
And I have an "index.html" page that contains "{% for member in site.data.members %}{{member.name}}{% endfor %}"
When I run jekyll build
Then the "_site/index.html" file should exist
And I should see "Jack" in "_site/index.html"
And I should see "Leon" in "_site/index.html"
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 }}
btorb
December 30, 2018, 4:06pm
3
How to proceed with the filter idea?
{{ site.data.images | where:"filename","jekyll-search.png" }}
What command is next to, say, access the width field of that entry?
(I have found a solution using a for loop on stack overflow , but directly accessing variables without a for loop seems much cleaner and faster with large data files.