0
votes

I have a Jekyll site with a _data directory full of files organized by date:

/_data/
   mydata_2020-08-28.json
   mydata_2020-08-29.json
   mydata_2020-08-30.json
   mydata_2020-08-31.json

I'd like to pull data from these files based on today's date, but I can't quite seem to get it right.

Let's say that I want to pull data from {{ site.data.mydata_2020-08-31.somefield }}, but I want to dynamically generate the date (YYYY-MM-DD) as today's date using {{ 'now' | date: "%Y-%m-%d" }}. I need to insert the date object into the data file object, but I can't find a way to do it.

I've tried:

{{ site.data.mydata_{{ 'now' | date: "%Y-%m-%d" }}.somefield }}

but it returns the truncated end of my liquid object:

.somefield

And I tried:

{{site.data.mydata_| append: 'now' | date: "%Y-%m-%d" | append:.somefield}}

which returns today's date:

2020-08-31

I also read through most of the documentation at https://shopify.github.io/liquid/, but maybe I'm overlooking something?

1

1 Answers

0
votes

If you want to fetch data based on the date in your case, try with the following:

{% assign today_data = 'now' | date: "%Y-%m-%d" | prepend: 'mydata_' %}

# You can fetch data file with particular date as follows:
{{ site.data[today_data] }}

NOTE: .somefield can't be fetched in above approach.