Search code examples
jekyllgithub-pagesliquid

Convert a Jekyll Liquid variable (string) to _data file path


I'm constructing a Jekyll site on Github pages. I have a Github action that delivers fresh daily data to my _data/ file, with today's timestamp:

_data/
   somedata_YYYYMMDD.json

I can construct a variable in a few ways to capture the filename for today's data file:

{% assign today_data = 'now' | date: "%Y%m%d" | prepend: 'site.data.somedata_' %}

or

{% capture today_data %}
site.data.somedata_{{ 'now' | date: "%Y%m%d" }}
{% endcapture %}

In both of these cases, if I try to put the today_data variable to use, Liquid interprets it as an inert string, rather than as a pointer to a liquid object. So, if I try {{today_data}}, I get the string "site.data.somedata_20200902", but I'd like it to return the contents of the json file.

I consulted a few other questions, but they don't seem to apply correctly to this situation:


Solution

  • Almost there, that is a string in both those cases. So it's effectively:

    {{ 'site.data.somedata_YYYYMMDD' }}
    

    Instead, you could do this to get access to and print the object:

    {% assign today_path = 'now' | date: "%Y%m%d" | prepend: 'somedata_' %}
    {{ site.data[today_path] | jsonify }}
    

    This way you are accessing the site.data object, and dynamically constructing the key/path.